v icon indicating copy to clipboard operation
v copied to clipboard

strconv: number formatting with thousands separators

Open floscodes opened this issue 2 years ago • 10 comments

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"

floscodes avatar Sep 10 '22 22:09 floscodes

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]}

shove70 avatar Sep 11 '22 00:09 shove70

thousands separators has been left unimplemented for a long time.

ghost avatar Sep 11 '22 08:09 ghost

So then I shall close this?

floscodes avatar Sep 11 '22 08:09 floscodes

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.

ghost avatar Sep 11 '22 08:09 ghost

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...

floscodes avatar Sep 11 '22 08:09 floscodes

Please v run v fmt -w on all the files.

Delta456 avatar Sep 11 '22 08:09 Delta456

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?

floscodes avatar Sep 11 '22 09:09 floscodes

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?

Yes, it would be better.

Delta456 avatar Sep 11 '22 10:09 Delta456

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?

Yes, it would be better.

Done. I renamed the functions due to redeclaration-conflicts.

floscodes avatar Sep 11 '22 10:09 floscodes

I ran v fmt -w and added the docs in the comments… shall I change something?

floscodes avatar Sep 16 '22 16:09 floscodes

Obviously there were some network errors again… @Delta456 may I ask you to run the checks again?

floscodes avatar Sep 22 '22 08:09 floscodes

Obviously there were some network errors again… @Delta456 may I ask you to run the checks again?

Sure I will

Delta456 avatar Sep 22 '22 09:09 Delta456

Thank you, @Delta456 . It seems that the errors will not resolve... I don't know why. Shall I make a new commit?

floscodes avatar Sep 22 '22 13:09 floscodes

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

larpon avatar Sep 22 '22 18:09 larpon

Mmhhh... I will try a force push...

floscodes avatar Sep 22 '22 19:09 floscodes

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 avatar Sep 22 '22 21:09 runeimp

@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.

ylluminate avatar Sep 24 '22 03:09 ylluminate

I will create a new one hoping that the errors won't occur anymore...

floscodes avatar Sep 26 '22 17:09 floscodes