Skip to main content
Dileep_NR
Inspiring
April 28, 2010
Answered

Why Number displayed in scientific notation

  • April 28, 2010
  • 2 replies
  • 2589 views

I have tried to add two numbers and got a result displayed in scientific notation.

Then I have used the 'decimalformat()' function and result is wrong.

How can display the correct result. Please help

Please see the code I have used

<cfset N1=1>

<cfset N2=9999999999999899999999>

<cfset RESULT = N1+ N2>

<cfoutput>

#RESULT#

<br />

#decimalformat(RESULT)#

</cfoutput>

result :

1E+022
92,233,720,368,547,760.00

Thanks in advance

    This topic has been closed for replies.
    Correct answer BKBK

    <cfset N1= createobject("java","java.math.BigInteger").init("1")>


    <cfset N2 = createobject("java","java.math.BigInteger").init("9999999999999899999999")>

    <cfset RESULT = N1.add(N2).toString()>

    <cfoutput>#result#</cfoutput>

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    April 28, 2010

    <cfset N1= createobject("java","java.math.BigInteger").init("1")>


    <cfset N2 = createobject("java","java.math.BigInteger").init("9999999999999899999999")>

    <cfset RESULT = N1.add(N2).toString()>

    <cfoutput>#result#</cfoutput>
    Dileep_NR
    Dileep_NRAuthor
    Inspiring
    April 30, 2010

    Hi BKBK,

              THANKS for your help.

    BKBK
    Community Expert
    Community Expert
    April 30, 2010

    OK.

    Inspiring
    April 28, 2010

    Because the maximum value for an integer in CF is (2^31)-1 (this will be depended on your JVM, I guess).  Which is 2147483647: far less than 9999999999999899999999, so 9999999999999899999999 needs to be stored as a DOUBLE, which is represented as a precision & a scale.

    It might pay to read up on how numbers are stored in computing.

    --
    Adam

    Dileep_NR
    Dileep_NRAuthor
    Inspiring
    April 28, 2010

    Thanks for your reply,

    But how we manage this?