Solved

Number Notation

  • 1 November 2016
  • 8 replies
  • 16 views

Badge +3

Good morning,

How could I transform a number into French Notation?

Ex: 1000000.00 into 1 000 000,00

This information represents surface area on a map. I have already worked out the calculations and the placement transformers and am stuck on the customer's desired presentation.

Thank you,

Chris

icon

Best answer by takashi 1 November 2016, 23:48

View original

8 replies

Badge +16

Hi @ccomeau I would look into the StringFormatter for a possible solution.

Hope this helps

Badge +16

Hi @ccomeau, had a look at the StringFormatter but couldn't get it working so a couple of AttributeSplitters and an AttributeManager did the job.

french-notation.fmw

Userlevel 2
Badge +17

Hi @ccomeau, a PythonCaller with this script might help you.

# PythonCaller Script Example: Format a numeric value in French Notation
# Assuming that the feature has an attribute called "value" storing a numeric value.
# Format the numeric value using comma as thousands separator,
# replace commas with spaces, and then replace the decimal point with a comma.
def processFeature(feature):
    attr = 'value'
    v = float(feature.getAttribute(attr))
    s = '{0:,.2f}'.format(v).replace(',', ' ').replace('.', ',')
    feature.setAttribute(attr, s)
Userlevel 2
Badge +17

Hi @ccomeau, a PythonCaller with this script might help you.

# PythonCaller Script Example: Format a numeric value in French Notation
# Assuming that the feature has an attribute called "value" storing a numeric value.
# Format the numeric value using comma as thousands separator,
# replace commas with spaces, and then replace the decimal point with a comma.
def processFeature(feature):
    attr = 'value'
    v = float(feature.getAttribute(attr))
    s = '{0:,.2f}'.format(v).replace(',', ' ').replace('.', ',')
    feature.setAttribute(attr, s)
another solution taking advantage of FME String Functions and FME Math Functions.

 

format-number-with-french-notation.fmw (FME 2016.1.2)

 

0684Q00000ArMf9QAF.png

Userlevel 2
Badge +17

Hi @ccomeau, a PythonCaller with this script might help you.

# PythonCaller Script Example: Format a numeric value in French Notation
# Assuming that the feature has an attribute called "value" storing a numeric value.
# Format the numeric value using comma as thousands separator,
# replace commas with spaces, and then replace the decimal point with a comma.
def processFeature(feature):
    attr = 'value'
    v = float(feature.getAttribute(attr))
    s = '{0:,.2f}'.format(v).replace(',', ' ').replace('.', ',')
    feature.setAttribute(attr, s)
At first, I thought this script does the trick.

 

import locale
class FeatureProcessor(object):
    def __init__(self):
        locale.setlocale(locale.LC_NUMERIC, 'French')
        
    def input(self,feature):
        attr = 'value'
        v = float(feature.getAttribute(attr))
        feature.setAttribute(attr, locale.format_string('%.2f', v, True))
        self.pyoutput(feature)
However, the result is shown like this in my environment - Japanese Windows.

 

1?000?000,00
I'm curious how the script works in an European locale.

 

Badge +3

Good morning,

Thank you both for the quick replies. Your FMWs reminded me that I need to update my licence from 2015.1.0.3 as some transformers are not available.

@itay, the FMW you posted works well with millions but smaller and larger numbers do not format correcly. EX. 235 came out as 2 35 . I would need to split from the right. Perhaps flipping the number around then splitting if, then flipping the lists around would work.

@takashi, I had not thought of using Python. Interesting solution option. Sadly, I am a novice programmer and can't get your first solution working. That being said, your second option using the local settings was a novel solution that did work.

Badge +16

Good morning,

Thank you both for the quick replies. Your FMWs reminded me that I need to update my licence from 2015.1.0.3 as some transformers are not available.

@itay, the FMW you posted works well with millions but smaller and larger numbers do not format correcly. EX. 235 came out as 2 35 . I would need to split from the right. Perhaps flipping the number around then splitting if, then flipping the lists around would work.

@takashi, I had not thought of using Python. Interesting solution option. Sadly, I am a novice programmer and can't get your first solution working. That being said, your second option using the local settings was a novel solution that did work.

Hi, true, a remedy would be to test the string lenght beforehand, but that saiud @taskashi 's answer is much more elegant.
Userlevel 2
Badge +17

Hi @ccomeau, a PythonCaller with this script might help you.

# PythonCaller Script Example: Format a numeric value in French Notation
# Assuming that the feature has an attribute called "value" storing a numeric value.
# Format the numeric value using comma as thousands separator,
# replace commas with spaces, and then replace the decimal point with a comma.
def processFeature(feature):
    attr = 'value'
    v = float(feature.getAttribute(attr))
    s = '{0:,.2f}'.format(v).replace(',', ' ').replace('.', ',')
    feature.setAttribute(attr, s)
The regex approach provided by @david_r might be a smarter solution. See this link.

 

Format number from: 1000000 to 1 000.000

 

Reply