4

Say I have 105.6546 and 5646.546356, in string or float fields, how can I drop only the decimal and values to the right of the decimal? So my final values will be 105 and 5646.

Particularly within FME.

4
  • What type of data object are those values in that you want to change?
    – artwork21
    Commented Mar 3, 2017 at 19:35
  • String. If I need to convert it to to numeric I can, but hoping there is someway to delete decimal and everything to the right of the decimal.
    – Kate
    Commented Mar 3, 2017 at 19:52
  • In FME none of the attribute types are really enforced inside the workspace (only when reading and writing). So whatever comes in as a number can be handled as a string - or vice versa - as long as the content is suitable. Commented Mar 3, 2017 at 19:57
  • If the values in the field are reliably of the form XXXX.YYYYY then another method may be the AttributeSplitter with the decimal point as the delimiter. You'll end up with a list (which you can name in the transformer options) holding XXXX as item (element) 1 and YYYYY as item (element) 2.
    – James
    Commented Mar 6, 2017 at 10:47

3 Answers 3

8

Using your Example you can use the Attribute Rounder

This example reads value from a field DP and takes the decimal out.

enter image description here

Note there is an option in the Attribute Rounder to go to the nearest value or go down or up in rounding.

3
  • Note there is an option in the Attribute Rounder to go to the nearest value or go down or up in rounding.
    – Mapperz
    Commented Mar 3, 2017 at 20:01
  • 2
    Yes, so use Down and not Nearest and it will work. Not sure why I didn't think of that! Commented Mar 3, 2017 at 20:03
  • Another option is using ExpressionEvaluator and using the Math Function floor(). Thanks Mapperz!
    – Kate
    Commented Mar 3, 2017 at 20:21
6

Should be fairly easy. My first thought is to treat it as a string and use a StringReplacer transformer.

Set Mode to "Replace Regular Expression" and set Text to Replace to

\..*$

ie remove content starting with the decimal (\.) and then any other characters (.*) at the end of the string ($).

And that should remove the decimal and fraction.

2

Just for yet another method to accomplish this task in FME, I thought I'd throw in a Python method. By no means is this the way I would usually truncate an attribute, I'd probably use @Mapperz's method. However, the option is there and Python is super powerful for dealing with attributes.

You can use a PythonCaller transformer to return the truncated value.

What I did was create an attribute called "Number" which holds the value I want to truncate. I also created an attribute called "Truncated" which will hold the truncated value. It's perfectly valid to just overwrite the Number with the truncated value and not have a second attribute.

enter image description here

Next, I pass the features into a PythonCaller. Where it says "Class of Function to Process Features, type in 'processFeature'. This is the name of the function which will truncate the number. You'll edit that in the next step.

enter image description here

Click on the ... to edit the Python Script. My script looks like this (ensure you import math): enter image description here

Here's the code:

import fme
import fmeobjects
import math

def processFeature(feature):
    truncated = math.trunc(float(feature.getAttribute('Number')))
    feature.setAttribute('Truncated', truncated)

If you just wanted to overwrite the original attribute and not have a second attribute called "Truncated", your last line of code would simply be:

feature.setAttribute('Number', truncated)

Note, I added float to convert the text to a number. If your data was numeric already, you wouldn't need float.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.