fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.
Fuzion
•
Idioms
•
Idiom # 173: Format a number with grouped thousands
Idiom # 173: Format a number with grouped thousands
See
programming-idioms.org
:
Code
format(x T : integer) String => str:= x.as_string len := str.byte_length first := if x < T.zero && len%3 = 1 then 4 # avoid separator right after minus else if len%3 > 0 then len%3 # determine size of first group else 3 groups(start i32, str String) String => if start > (str.byte_length - 3) then "" else "," + (str.substring start (start + 3)) + groups (start + 3) str (str.substring (0) (first)) + (groups first str)
What are effects?
Running Example
ex173 is format(x T : integer) String => str:= x.as_string len := str.byte_length first := if x < T.zero && len%3 = 1 then 4 # avoid separator right after minus else if len%3 > 0 then len%3 # determine size of first group else 3 groups(start i32, str0 String) String => if start > (str0.byte_length - 3) then "" else "," + (str0.substring start (start + 3)) + groups (start + 3) str0 (str.substring (0) (first)) + (groups first str) numbers := [1, 42, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789] for n in numbers do say "$n -> {format n}" for n in numbers do n_neg := n*(-1); say "$n_neg -> {format n_neg}" a := int i64.max say (format a*a)
What are effects?
last changed: 2024-07-01
next: NYI: Idiom # 174: Make HTTP POST request