Skip to main content
Solved

Format number from: 1000000 to 1 000.000

  • March 1, 2017
  • 6 replies
  • 109 views

sigtill
Supporter
Forum|alt.badge.img+25

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))
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

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
Celebrity
  • 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_defra
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
Celebrity
  • 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?

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_defra
Contributor
Forum|alt.badge.img+5
  • Contributor
  • 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))
@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
Celebrity
  • March 1, 2017
@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.