Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Digit grouping #11734

Open
ghost opened this issue Jul 15, 2019 · 16 comments
Open

Digit grouping #11734

ghost opened this issue Jul 15, 2019 · 16 comments

Comments

@ghost
Copy link

ghost commented Jul 15, 2019

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
Copy link
Member

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

strutils.insertSep

@mratsim
Copy link
Collaborator

mratsim commented Jul 15, 2019

Probably an option to add to strformat formatting of numbers

@Araq
Copy link
Member

Araq commented Jul 15, 2019

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

@ghost ghost changed the title Number format Digit grouping Jul 15, 2019
@ghost ghost closed this as completed Jul 15, 2019
@ghost ghost reopened this Jul 15, 2019
@qqtop
Copy link

qqtop commented Jul 16, 2019

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
Copy link
Author

ghost commented Jul 16, 2019

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

@qqtop
Copy link

qqtop commented Jul 16, 2019

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
Copy link
Contributor

krux02 commented Jul 18, 2019

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

@ghost
Copy link
Author

ghost commented Jul 18, 2019

@narimiran I just noticed an issue:

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

Result:

1,234,.56

@narimiran
Copy link
Member

narimiran commented Jul 18, 2019

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
Copy link
Contributor

krux02 commented Jul 19, 2019

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

@ghost
Copy link
Author

ghost commented Jul 19, 2019

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

@ringabout ringabout self-assigned this Apr 3, 2021
@timotheecour
Copy link
Member

for the reverse direction, see #15421 (comment)

@ringabout
Copy link
Member

@timotheecour
Copy link
Member

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
Copy link
Member

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

@ringabout ringabout removed their assignment Apr 4, 2021
@ringabout
Copy link
Member

using sprintf make it a bit hard to extend.

needs
#13365
to move on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants