Commas in Numbers
From Erlang Community
(Difference between revisions)
| Revision as of 23:15, 3 September 2006 (edit) Bfulgham (Talk | contribs) ← Previous diff |
Revision as of 12:43, 24 September 2006 (edit) (undo) Ayrnieu (Talk | contribs) (ah, I put this first in discussion, but nevermind: I hate the given solution. Also, they aren't "Perl-compatible regular expressions".) Next diff → |
||
| Line 5: | Line 5: | ||
| == Solution == | == Solution == | ||
| - | + | Turn the number into a list and then use pattern-matching to insert commas where needed. | |
| + | |||
| <code> | <code> | ||
| - | + | partition_integer(N,P) -> | |
| - | | + | partition_integer(lists:reverse(integer_to_list(N)),P,[]). |
| - | + | ||
| - | + | partition_integer([A,B,C,D|T],P,Acc) -> | |
| - | + | partition_integer([D|T],P,[P,C,B,A|Acc]); | |
| - | + | partition_integer(L,_,Acc) -> | |
| - | + | lists:reverse(L) ++ Acc. | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | | + | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | lists:reverse( | + | |
| </code> | </code> | ||
| - | + | Floating-point numbers generally require additional separator, and also an explicit fractional-part length: | |
| - | Here's an example of partition_number in action: | ||
| <code> | <code> | ||
| - | 1> | + | 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]). | ||
| + | </code> | ||
| + | |||
| + | Here's an example of the above in action: | ||
| + | <code> | ||
| + | 1> partition_integer(1918928282, $,). | ||
| "1,918,928,282" | "1,918,928,282" | ||
| - | 2> | + | 2> partition_integer(1982928828, $'). |
| "1'982'928'828" | "1'982'928'828" | ||
| - | 3> | + | 3> partition_float(1982928828.12345, $', $., 6). |
| "1'982'928'828.123450" | "1'982'928'828.123450" | ||
| - | </code> | + | </code> |
| - | + | ||
| - | + | ||
| [[Category:CookBook]][[Category:StringRecipes]][[Category:ListRecipes]][[Category:NumberRecipes]] | [[Category:CookBook]][[Category:StringRecipes]][[Category:ListRecipes]][[Category:NumberRecipes]] | ||
Revision as of 12:43, 24 September 2006
Problem
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.
Solution
Turn the number into a list and then use pattern-matching to insert commas where needed.
partition_integer(N,P) ->
partition_integer(lists:reverse(integer_to_list(N)),P,[]).
partition_integer([A,B,C,D|T],P,Acc) ->
partition_integer([D|T],P,[P,C,B,A|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)])))),
lists:flatten([partition_integer(trunc(N),P),FP,F]).
|
Here's an example of the above in action:
1> partition_integer(1918928282, $,). "1,918,928,282" 2> partition_integer(1982928828, $'). "1'982'928'828" 3> partition_float(1982928828.12345, $', $., 6). "1'982'928'828.123450" |

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

