0

我有一个像 10 位数字的金额,"1234567328.899"我想用逗号显示为货币,如下所示:。

"$1,234,567,328.899". 

我写了一个独立的模块来做到这一点,它正在工作

partition_float(N,P,FP,L) ->
    F = tl(tl(hd(io_lib:format("~.*f",[L,N-trunc(N)])))),
    lists:flatten([partition_integer(trunc(N),P),FP,F]).

partition_float(Data, $,, $., 6).%% Where Data is ["1234567328.899", ""1217328.89", "67328", ...]

它作为独立运行成功,但是当插入到芝加哥老板的项目时,它会抛出以下错误。

[{"c:/Users/Dey/InheritanceTax/inheritance_tax/src/lib/data_util.erl",[{575,erl_lint,{call_to_redefined_old_bif,{trunc,1}}},{576,erl_lint,{call_to_redefined_old_bif,{trunc,1}}}]}]
4

2 回答 2

2

对于正好六个字符的数字,那里有一个小错误。下面是一个小调整。如果有机会,我将发布一个更通用形式的此方法的最终更新,该方法可以接受数字、字符串或二进制输入,并确定是否需要保留小数位以及保留多少位。

-module(format).
-export([currency/1]).

format_decimal_number(S, Acc, M) ->
  try
    Str = string:substr(S, 1, 3),
    Str2 = string:substr(S, 4),
    Str1 = if
             length(Str2) > 0 ->  Str ++ ",";
             true -> Str
           end,
    format_decimal_number(Str2, Acc ++ Str1, M)
  catch
    error:_Reason ->
      io_lib:format("~s", [lists:reverse(Acc ++ S ++ "$") ++ "." ++ M])
  end.


format_integer_number(S, Acc) ->
  try
    Str = string:substr(S, 1, 3),

    Str2 = string:substr(S, 4),
    Str1 = if
       length(Str2) > 0 ->  Str ++ ",";
       true -> Str
    end,
    format_integer_number(Str2, Acc ++ Str1)
  catch
    error:_Reason ->
      io_lib:format("~s", [lists:reverse(Acc ++ S ++ "$") ])
  end.


currency(N) ->
  format_integer_number(lists:reverse(N), "").

currency_decimal(N) ->
  [L,M] = string:tokens(N, "."),
  format_decimal_number(lists:reverse(L), "", "").
于 2014-10-14T12:52:32.037 回答
1
-module(amt).
-export([main/0]).

format(S, Acc, M) ->
    try 
        Str = string:substr(S, 1, 3),
        Str1 = Str ++ ",",
        Str2 = string:substr(S, 4),
        format(Str2, Acc ++ Str1, M),
        ok
    catch
        error:_Reason ->
            % io:format("~p~n", [Reason]),
            io:format("~p", [lists:reverse(Acc ++ S ++ "$") ++ "." ++ M])
    end.

disp(N) ->
    [L,M] = string:tokens(N, "."),
    format(lists:reverse(L), "", M).

main() -> disp("1234567328.899").   % "$1,234,567,328.899"ok
于 2014-03-27T10:12:59.807 回答