我有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", ...]
它作为一个独立的成功运行但是当插入一个chicagoboss的项目时,它会抛出以下错误。
[{"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}}}]}]
答案 0 :(得分: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), "", "").
答案 1 :(得分: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