Skip to main content
Solved

jsontemplater turns 2-decimal number 1.21 into 1.2100000381469727

  • April 16, 2026
  • 8 replies
  • 102 views

arnovananrooij
Contributor
Forum|alt.badge.img+5

Hi,

I use a JSONTemplater to make a geojson with nested attributes. It works fine. The only thing I notice is that I have a number in real32 (1.21) and when it comes out of the JSONTemplater it has the value 1.2100000381469727. The extra decimals apear out of nowhere. How can I change these back to the original? Or can I use some kind of casting? 

I use FME 2023.2.5.

Thank you

Best answer by david_r

Is it possible that your value is either null or missing when you get this error?

Try using something like this in the JSONTemplater, which will handle null and empty values:

{
"value": if (fme:get-attribute("value") castable as xs:double)
then fn:round(xs:double(fme:get-attribute("value")), 2)
else ()
}

 

8 replies

david_r
Celebrity
  • April 16, 2026

You can use XQuery expressions inside the JSONTemplater. Example rounding the attribute "value" to two decimals:

{ "value":{fn:round(xs:double(fme:get-attribute("value")), 2)} }
Result:

{ "value" : 1.23 }

You can find a reference of all the XQuery and XPath functions here: https://www.w3.org/TR/xpath-functions-31/ 


s.jager
Influencer
Forum|alt.badge.img+24
  • Influencer
  • April 16, 2026

Hoi Arno,

Both the JSONTemplater and the XMLTemplater “suffer” from this problem. There is a good explanation for it though, which you can find in this thread: XMLtermplater vs. XSD-driven and a little problem | Community


david_r
Celebrity
  • April 16, 2026

Just to provide some background, this “problem” is not related to FME but to floating point precision in general, and how computers represent floating point numbers internally. Basically, floating point (real) numbers cannot, in general, be accurately represented in fixed space such as binary. This means that all floating point rounding errors cannot be eliminated, only managed.

Some systems go to great lengths to hide (manage) these imperfections from the users, including Python and FME, but we can sometimes, inadvertently, cause cracks in the façace and we end up with the real, internal value, and you get something like 1.2100000381469727 rather than the expected 1.21.

This is a fairly good introduction: 

 


arnovananrooij
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • April 23, 2026

You can use XQuery expressions inside the JSONTemplater. Example rounding the attribute "value" to two decimals:

{ "value":{fn:round(xs:double(fme:get-attribute("value")), 2)} }
Result:

{ "value" : 1.23 }

You can find a reference of all the XQuery and XPath functions here: https://www.w3.org/TR/xpath-functions-31/ 

Hi ​@david_r , 

Thank you for your reply.

I tried your suggestion, bur this gives an error: JSONTemplater_2 (XMLTemplaterFactory): "xs:string": invalid value for cast/constructor: can not cast to "xs:double". How can I fix this.


david_r
Celebrity
  • Best Answer
  • April 23, 2026

Is it possible that your value is either null or missing when you get this error?

Try using something like this in the JSONTemplater, which will handle null and empty values:

{
"value": if (fme:get-attribute("value") castable as xs:double)
then fn:round(xs:double(fme:get-attribute("value")), 2)
else ()
}

 


arnovananrooij
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • April 23, 2026

Hoi Arno,

Both the JSONTemplater and the XMLTemplater “suffer” from this problem. There is a good explanation for it though, which you can find in this thread: XMLtermplater vs. XSD-driven and a little problem | Community

Hi ​@s.jager  I had a look at the thread you pointed to and tried {fn:format-number(xs:double(fme:get-attribute("numberAttribute")), "0.00")} instead of the fn:round suggestion by David. This also gives me an error:  "xs:string": invalid value for cast/constructor: can not cast to "xs:double". 

Maybe the problem in my case is that the attribute can be null.


arnovananrooij
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • April 23, 2026

You can use XQuery expressions inside the JSONTemplater. Example rounding the attribute "value" to two decimals:

{ "value":{fn:round(xs:double(fme:get-attribute("value")), 2)} }
Result:

{ "value" : 1.23 }

You can find a reference of all the XQuery and XPath functions here: https://www.w3.org/TR/xpath-functions-31/ 

Hi ​@david_r , 

Thank you for your reply.

I tried your suggestion, bur this gives an error: JSONTemplater_2 (XMLTemplaterFactory): "xs:string": invalid value for cast/constructor: can not cast to "xs:double". How can I fix this.

Hi ​@david_r , I solved it by adding a NullAttributeMapper to convert every null and empty value into missing and used your solution in de JSONTemplater. 

Thank you


arnovananrooij
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • April 23, 2026

Hoi Arno,

Both the JSONTemplater and the XMLTemplater “suffer” from this problem. There is a good explanation for it though, which you can find in this thread: XMLtermplater vs. XSD-driven and a little problem | Community

Hi ​@s.jager  I had a look at the thread you pointed to and tried {fn:format-number(xs:double(fme:get-attribute("numberAttribute")), "0.00")} instead of the fn:round suggestion by David. This also gives me an error:  "xs:string": invalid value for cast/constructor: can not cast to "xs:double". 

Maybe the problem in my case is that the attribute can be null.

@s.jager  I added a NullAttributeMapper to convert all null values to missing. Then your solution works, but it sets all values into two decimals so I used fn:round suggested by David, But thanks for your suggestion.