Skip to main content
Solved

Format number from: 1000000 to 1 000.000


sigtill
Contributor
Forum|alt.badge.img+24

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?

Best answer by david_r

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))
View original
Did this help you find an answer to your question?

6 replies

Forum|alt.badge.img+7

Hi @sigtill

I think you will find an answer here:

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


david_r
Evangelist
  • Best Answer
  • March 1, 2017

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

nrich
Contributor
Forum|alt.badge.img+5
  • Contributor
  • March 1, 2017

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?


david_r
Evangelist
  • March 1, 2017
nrich wrote:

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.

nrich
Contributor
Forum|alt.badge.img+5
  • Contributor
  • March 1, 2017
david_r wrote:

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+$)

 


david_r
Evangelist
  • March 1, 2017
nrich wrote:
@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.

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings