v
v copied to clipboard
strconv: number formatting with thousands separators
This contribution adds the possibility to format numbers (integers, floats) with thousands separators and with a custom-set radix.
// f32_format - Formats an f32-number as a string by adding thousands-separators and a custom-set radix. The first argument represents the number you want to format, the second represents the thousands-separator as a string and the third represents the radix also as a string.
Example:
assert strings.f32_format(1000.48, ",", ".") == "1,000.48"
// f64_format - Formats an f64-number as a string by adding thousands-separators and a custom-set radix. The first argument represents the number you want to format, the second represents the thousands-separator as a string and the third represents the radix also as a string.
Example:
assert strings.f64_format(10000.4567657657, ",", ".") == "10,000.4567657657"
// int_format - Formats an integer (i16 or i32 or i64) by adding thousands-separators and returns a string. The first argument represents the integer you want to format, the second represents the thousands-separator as a string.
Example:
assert strings.int_format(1000000, ",") == "1,000,000"
// uint_format - Formats an unsigned integer (u16 or u32 or u64) by adding thousands-separators and returns a string. The first argument represents the integer you want to format, the second represents the thousands-separator as a string.
Example:
assert strings.uint_format(1000000, ",") == "1,000,000"
It seems to be gradually unified as string injection
:
https://github.com/vlang/v/blob/master/doc/docs.md#string-interpolation ${varname:[flags][width][.precision][type]}
thousands separators has been left unimplemented for a long time.
So then I shall close this?
I think no one has tried to implement this before because of the location and other factors involved. I personally would like to see thousand separators available as a standard feature.
Ok, yes I also would like to see that.
And regarding the location I thought it would be nice to set customized separator and radix. That's why I implemented this...
Please v run v fmt -w
on all the files.
Please v run
v fmt -w
on all the files.
Done... But should I add this feature to strconv
rather than the strings
-module? What do you think?
Please v run
v fmt -w
on all the files.Done... But should I add this feature to
strconv
rather than thestrings
-module? What do you think?
Yes, it would be better.
Please v run
v fmt -w
on all the files.Done... But should I add this feature to
strconv
rather than thestrings
-module? What do you think?Yes, it would be better.
Done. I renamed the functions due to redeclaration-conflicts.
I ran v fmt -w and added the docs in the comments… shall I change something?
Obviously there were some network errors again… @Delta456 may I ask you to run the checks again?
Obviously there were some network errors again… @Delta456 may I ask you to run the checks again?
Sure I will
Thank you, @Delta456 . It seems that the errors will not resolve... I don't know why. Shall I make a new commit?
Vinix build is failing because if it: https://github.com/vlang/v/actions/runs/3106006434/jobs/5032333261#step:6:16
Also doc generation: https://github.com/vlang/v/actions/runs/3106006434/jobs/5032333261#step:4:122
It's not because of network errors in these runs, don't know why they fail
Mmhhh... I will try a force push...
International number formatting is a fair bit more complex than what exists in just the US and GB. Numbers in India is a good example and why I ended up creating https://github.com/runeimp/locale for Go on behalf of another app of mine. It would be ideal if all the international formats were supported in V so such a library isn't needed just to be viable for over a billion people in the world.
@runeimp please feel free to make a PR at https://github.com/vlang/rfcs and/or expand that to some implementation here in V proper. With the "batteries included" approach of V your suggestion seems plausible on the outset.
I will create a new one hoping that the errors won't occur anymore...