I strongly encourage the practice of NEVER converting strings with decimal values to float/double types. Float/double have a fundamental behavior that can really mess up monetary calculations. The challenge is that the behaviors only show up in some instances and can be hard to track down. The basic issue: the decimal fraction 0.1 is a repeating fraction in binary. It cannot be accurately represented. The usual things people attempt is truncating and rounding after performing a calculation. But depending upon the calculation the "damage’ may have already been (silently) done with inaccurate results.
There are a couple of threads on these forums covering ways to avoid using float/double. One downside is that pub.json:jsonStringToDocument does not provide sufficient control to prevent all conversions to native types. If a numeric value in a JSON element is an integer, instead of a decimal, it will be converted to int/long by default. This doesn’t cause accuracy issues but I wish there was an option to keep it as string. JSON syntax distinguishes between “number” (contains decimal) and “integer” (no decimal) but practically they usually need to be treated the same – a field may contain either “type” and still be valid. I wish jsonStringToDocument had a single setting to keep everything as string (JSON is fully a string representation with “hints” about numeric values). [EDIT] 10.15 and later addresses this. Thanks @John_Carter4 for the reminder!
An additional challenge is pub.json:documentToJSONString doesn’t provide control to create JSON elements as number/integer (no quotes around the value) without converting the values in the document from strings to native types or to wrapper classes. I wish it accepted a string list parameter naming the fields to be treated as number/integer (no quotes) to simplify things. Until then, we use pub.math:toNumber with convertAs set to java.math.BigDecimal or Integer/Long/BigInteger as needed (never Float/Double).
There is almost no reason to convert data elements to native types (float/double/long) in Integration Server. Even the pub.math services accept strings not float/double. BUT those services should be avoided because they convert strings to float/double, potentially introducing accuracy errors. We created equivalent services using BigDecimal within. This caveat applies to any calculations in any language – this is not unique to Java. If accuracy is needed (for money calcs, when isn’t it?) avoid float/double everywhere.
#Flow-and-Java-services#Integration-Server-and-ESB#webMethods