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.
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.
Using your Example you can use the Attribute Rounder
This example reads value from a field DP and takes the decimal out.
Note there is an option in the Attribute Rounder to go to the nearest value or go down or up in rounding.
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.
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.
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.
Click on the ...
to edit the Python Script. My script looks like this (ensure you import math):
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
.