0

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}}}]}]
Laxmikant Ratnaparkhi
  • 4,745
  • 5
  • 33
  • 49
  • 4
    First of all: never use floating point numbers to represent currency. Use integers representing cents (or 10ths/100ths/1000ths of a cent depending on the precision you need). Otherwise you'll end up with rounding errors that make it look like there's money missing. – RichardC Mar 26 '14 at 13:13
  • 3
    The `call_to_redefined_old_bif,{trunc,1}` part of the error suggests that the `trunc` function has been redefined - whether this redefinition is in chicagoboss or in a different version of Erlang (used when you transferred the code) I don't know... – Lee Mar 26 '14 at 13:36
  • @RichardC: Thanks for suggestion, could you please explain me in details with some example. – Laxmikant Ratnaparkhi Mar 27 '14 at 04:45
  • @atomh33ls: Thanks for suggest. Actually I know that it was redefined, how would we can find it? Is there any other way to do that? – Laxmikant Ratnaparkhi Mar 27 '14 at 04:47
  • You are using `io_lib` incorrectly as well. It is free to return something else. – I GIVE CRAP ANSWERS Mar 27 '14 at 09:58

2 Answers2

2

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), "", "").
Keith Brings
  • 241
  • 3
  • 5
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