Erlang Central

Commas in Numbers

Revision as of 00:24, 23 November 2006 by RJ (Talk | contribs)


You need to format a number for printing with commas (or other locale-dependant marks) in the appropriate places. This is important for creating human-legible print copies of reports.


Turn the number into a list and then use pattern-matching to insert commas where needed.

partition_integer(N,P) ->

partition_integer([A,B,C,D|T],P,Acc) ->
partition_integer(L,_,Acc) ->
    lists:reverse(L) ++ Acc.

Floating-point numbers generally require additional separator, and also an explicit fractional-part length:

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

Here's an example of the above in action:

1> partition_integer(1918928282, $,).
2> partition_integer(1982928828, $'). 
3> partition_float(1982928828.12345, $', $., 6).

Casino games. on line casinos online casino for cash Casino games. free online casino tournament online casino games Casinos security online. Gambling Online - Risks. fun casinos free casinos on line