I have an amount like 10 digits "1234567328.899" I want to show as currency with commas in it like this:.

"$1,234,567,328.899". 

I had written a standalone module to do that, it was working

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", ...]

It was running successfully as a stand alone but when insert to the project of a chicagoboss, It's throwing following error.

[{"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}}}]}]
有帮助吗?

解决方案 2

-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

其他提示

There's a small bug in there for digits of exactly six chars. Below is a minor tweak. If I get the chance I'll post a final update with a more generic form of this method that can accept numberica, string or binary input and determine if the decimal place needs to be preserved and to ho many places.

-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), "", "").
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top