Solved

Format number from: 1000000 to 1 000.000


Badge +21

Can you convert, with the Stringformatter from

integer = 1000000

integer = 1234567

integer = 234567

to

1 000.000

1 234.567

234.567

?

 

Or do you have to do some dirty python?

icon

Best answer by david_r 1 March 2017, 11:18

View original

6 replies

Badge

Hi @sigtill

I think you will find an answer here:

https://knowledge.safe.com/questions/37508/format-number-with-commas.html

Userlevel 4

The StringFormatter seems to be based on the C printf function (doc) which doesn't seem to have thousand separators as a format string. But you can use a StringReplacer:

0684Q00000ArKGcQAN.png

Modify the replacement text as needed. Regex for copy/paste purposes:

(?<=\d)(?=(\d\d\d)+(?!\d))
Badge

with this one, couldn't you also use an expression evaluator to divide by a thousand and return the double? or am I missing something?

Userlevel 4

with this one, couldn't you also use an expression evaluator to divide by a thousand and return the double? or am I missing something?

If I'm reading it correctly, there are two operations here:

 

  1. divide input number by 1000
  2. format the integer part with thousand separators
The StringReplacer above only does part 2.
Badge

The StringFormatter seems to be based on the C printf function (doc) which doesn't seem to have thousand separators as a format string. But you can use a StringReplacer:

0684Q00000ArKGcQAN.png

Modify the replacement text as needed. Regex for copy/paste purposes:

(?<=\d)(?=(\d\d\d)+(?!\d))
@david_r I think this regex adds commas to the decimal part if it's greater the 3 decimal places?  so 1234.5678 = 1234.567,8

 

 

had a tinker to combine your regex and mine (by which I mean the regex a plagerised) into this, which I think works for decimals, but not integers now!  I think given a bit more time we can tweak this to work with both, need to fix the grouping at the end.

 

 

(?<=\d)(?=(\d{3})+\.{1}\d+$)

 

Userlevel 4
@david_r I think this regex adds commas to the decimal part if it's greater the 3 decimal places? so 1234.5678 = 1234.567,8

 

 

had a tinker to combine your regex and mine (by which I mean the regex a plagerised) into this, which I think works for decimals, but not integers now! I think given a bit more time we can tweak this to work with both, need to fix the grouping at the end.

 

 

(?<=\\d)(?=(\\d{3})+\\.{1}\\d+$)

 

Good catch, forgot to check for that. Having said that, I'd much rather implement this in Python than as a regex, at least then you have a chance of understanding what's going on if you stumble upon it later on ;-)

 

The Python code I posted here works pretty well, even for this particular use case: https://knowledge.safe.com/questions/37508/format-number-with-commas.html

 

Feel free to turn it into a custom transformer and put it on the FME Hub, if you want.

Reply