Open ghost opened 5 years ago
Nim seems to have no way to format a number with commas or digit grouping.
Probably an option to add to strformat formatting of numbers
Well yes, but since it's deterministic, .replace('.', ',')
will always work well.
import nimcx
let a = 1234567890
let b = -10007887236.4567
echo ff2(a,2)
echo ff2(b,5)
gives
1,234,567,890.00
-10,007,887,236.45670
@qqtop thanks, but looks like that doesnt work on windows
nimcx is made for linux only but you are free to just extract the relevant procs and adapt / improve them for windows . The procs sit in cxglobal.nim
just a small remark: (1000.5).toLocaleString();
returns '1.000,5'
on my system. I don't know if you ever want that.
@narimiran I just noticed an issue:
import strutils
const aa = $(1234.56)
echo aa.insertSep(',')
Result:
1,234,.56
It is not an issue, it works as advertised: the algorithm works with any string s
, meaning it treats 123456
, 123.45
, abcdef
, !@#$%^
, etc. completely the same — it inserts a separator after n
characters from the right.
@cup can we mark this feauter request then as resolved?
@krux02 um, no? It would be resolved if it was in the standard library, or if the developers are currently officially refusing to do that.
for the reverse direction, see https://github.com/nim-lang/Nim/pull/15421#issuecomment-746863623
https://www.python.org/dev/peps/pep-0515/ is already in nim, so i guess it's mostly https://www.python.org/dev/peps/pep-0378/ to consider
yeah, pep515 also introduces _
option for f-string. Before f-string only support ,
option.
using sprintf
make it a bit hard to extend.
needs https://github.com/nim-lang/Nim/issues/13365 to move on.
Nim seems to have no way to format a number with commas or digit grouping. Other languages offer this, for example C:
Python:
JavaScript:
PHP:
Go:
D:
(EDIT) links