nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.59k stars 1.47k forks source link

Digit grouping #11734

Open ghost opened 5 years ago

ghost commented 5 years ago

Nim seems to have no way to format a number with commas or digit grouping. Other languages offer this, for example C:

#define _XOPEN_SOURCE 700
#include <locale.h>
#include <stdio.h>
int main() {
   setlocale(LC_ALL, "");
   printf("%'d\n", 1000);
}

Python:

f'{1000:,}'

JavaScript:

(1000).toLocaleString();

PHP:

number_format(1000);

Go:

package main
import "golang.org/x/text/language"
import "golang.org/x/text/message"
func main() {
   message.NewPrinter(language.English).Println(1000)
}

D:

import std.format, std.stdio;
void main() {
   auto aa = "%,d".format(1000);
   writeln(aa);
}

(EDIT) links

narimiran commented 5 years ago

Nim seems to have no way to format a number with commas or digit grouping.

strutils.insertSep

mratsim commented 5 years ago

Probably an option to add to strformat formatting of numbers

Araq commented 5 years ago

Well yes, but since it's deterministic, .replace('.', ',') will always work well.

qqtop commented 5 years ago
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
ghost commented 5 years ago

@qqtop thanks, but looks like that doesnt work on windows

qqtop commented 5 years ago

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

krux02 commented 5 years ago

just a small remark: (1000.5).toLocaleString(); returns '1.000,5' on my system. I don't know if you ever want that.

ghost commented 5 years ago

@narimiran I just noticed an issue:

import strutils
const aa = $(1234.56)
echo aa.insertSep(',')

Result:

1,234,.56
narimiran commented 5 years ago

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.

krux02 commented 5 years ago

@cup can we mark this feauter request then as resolved?

ghost commented 5 years ago

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

timotheecour commented 3 years ago

for the reverse direction, see https://github.com/nim-lang/Nim/pull/15421#issuecomment-746863623

ringabout commented 3 years ago

See also https://www.python.org/dev/peps/pep-0378 https://www.python.org/dev/peps/pep-0515

timotheecour commented 3 years ago

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

ringabout commented 3 years ago

yeah, pep515 also introduces _ option for f-string. Before f-string only support , option.

ringabout commented 3 years ago

using sprintf make it a bit hard to extend.

needs https://github.com/nim-lang/Nim/issues/13365 to move on.