How to convert large exponential values to integer/decimal format in Marklogic?

Viewed 165

I have an exponential value for e.g. 3.22122E+23

In Marklogic when I try- xs:decimal(3.22122E+23) I get this error:

[1.0-ml] XDMP-CAST: (err:FORG0001) xs:decimal(xs:double("3.22122E23")) -- Invalid cast: xs:double("3.22122E23") cast as xs:decimal

A lower value for e.g. xs:decimal(3.22122E+18) gives me the correct result i.e. 3221220000000000000.

I see that this is because of decimal overflow and cannot be represented as a decimal data type but is there any way in Marklogic to handle and calculate such huge values?

Same question applies for the negative values(3.22122E-23) where I can handle and display data above 20 decimal places.

1 Answers

It would be helpful to clarify what kind of logic or calculations you are trying to accomplish and why exactly you need to convert the value to decimal. For example, to "display" the double value, you can use the standard format-number function without any conversion to decimal:

let $x := xs:double(3.22122E+23)
return format-number($x,"#,##0.00")

yields:

322,122,000,000,000,000,000,000.00

See https://docs.marklogic.com/fn:format-number for details regarding fn:format-number() usage.

See https://help.marklogic.com/Knowledgebase/Article/View/487/0/marklogic-server-and-the-decimal-type-implementation for details of the limitations of the xs:decimal type.

Related