Scientific Notation with PrecisionEvaluate in Coldfusion

后端 未结 1 1931
离开以前
离开以前 2020-12-06 08:45

I have problems working with large numbers and long decimal numbers, as others have mentioned or solved such issue using PrecisionEvaluate, I could not get consistent result

1条回答
  •  星月不相逢
    2020-12-06 09:08

    When it comes to numbers, do not always believe what you see on the screen. That is just a "human friendly" representation of the number. In your case, the actual results (or numbers) are consistent. It is just a matter of how those numbers are presented ..

    PrecisionEvaluate returns a java.math.BigDecimal object. In order to display the number represented by that object inside , CF invokes the object's toString() method. Per the API, toString() may use scientific notation to represent the value. That explains why it is used for some of your values, but not others. (Though with or without the exponent, it still represents the same number). However, if you prefer to exclude the exponent, just use BigDecimal.toPlainString() instead:

    toPlainString() - Returns a string representation of this BigDecimal without an exponent field....

    Example:

    
        n = 0.000000000009;
        r = 12567.8903;
        result = precisionEvaluate(r * n);
    
        WriteOutput( result.getClass().name );
        WriteOutput("
    result.toString() ="& result.toString()); WriteOutput("
    result.toPlainString() ="& result.toPlainString());

    Result:

    java.math.BigDecimal
    result.toString() =1.131110127E-7
    result.toPlainString() =0.0000001131110127 
    

    0 讨论(0)
提交回复
热议问题