commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "sydng (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (LANG-1018) NumberUtils.createNumber(final String str) Precision will be lost
Date Wed, 28 May 2014 01:38:02 GMT

     [ https://issues.apache.org/jira/browse/LANG-1018?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

sydng updated LANG-1018:
------------------------

    Description: 
With commons-lang 3.2.2:
NumberUtils.createNumber("-160952.54");
The result is "-160952.55".

Should not be based on the length of the decimal point number to judge whether the floating
point number.
Using the method (createFloat(str)) of dealing with the valid number greater than seven Numbers
will cause accuracy loss.
The source code is as follows:

{color:#8bd1e2}
try {
            if(numDecimals <= 7){// If number has 7 or fewer digits past the decimal point
then make it a float
                final Float f = createFloat(str);
                if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                    return f;
                }
            }
        } catch (final NumberFormatException nfe) { // NOPMD
            // ignore the bad number
        }
        try {
            if(numDecimals <= 16){// If number has between 8 and 16 digits past the decimal
point then make it a double
                final Double d = createDouble(str);
                if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                    return d;
                }
            }
        } catch (final NumberFormatException nfe) { // NOPMD
            // ignore the bad number
        }

        return createBigDecimal(str);
    }
{color:#8bd1e2}

  was:
With commons-lang 3.2.2:
NumberUtils.createNumber("-160952.54");
The result is "-160952.55".

Should not be based on the length of the decimal point number to judge whether the floating
point number.
Using the method (createFloat(str)) of dealing with the valid number greater than seven Numbers
will cause accuracy loss.
The source code is as follows:
{color:#8bd1e2}
try {
            if(numDecimals <= 7){// If number has 7 or fewer digits past the decimal point
then make it a float
                final Float f = createFloat(str);
                if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                    return f;
                }
            }
        } catch (final NumberFormatException nfe) { // NOPMD
            // ignore the bad number
        }
        try {
            if(numDecimals <= 16){// If number has between 8 and 16 digits past the decimal
point then make it a double
                final Double d = createDouble(str);
                if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                    return d;
                }
            }
        } catch (final NumberFormatException nfe) { // NOPMD
            // ignore the bad number
        }

        return createBigDecimal(str);
    }
{color:#8bd1e2}


> NumberUtils.createNumber(final String str)  Precision will be lost
> ------------------------------------------------------------------
>
>                 Key: LANG-1018
>                 URL: https://issues.apache.org/jira/browse/LANG-1018
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 3.3.2
>         Environment: windows 7
>            Reporter: sydng
>
> With commons-lang 3.2.2:
> NumberUtils.createNumber("-160952.54");
> The result is "-160952.55".
> Should not be based on the length of the decimal point number to judge whether the floating
point number.
> Using the method (createFloat(str)) of dealing with the valid number greater than seven
Numbers will cause accuracy loss.
> The source code is as follows:
> {color:#8bd1e2}
> try {
>             if(numDecimals <= 7){// If number has 7 or fewer digits past the decimal
point then make it a float
>                 final Float f = createFloat(str);
>                 if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros)))
{
>                     return f;
>                 }
>             }
>         } catch (final NumberFormatException nfe) { // NOPMD
>             // ignore the bad number
>         }
>         try {
>             if(numDecimals <= 16){// If number has between 8 and 16 digits past the
decimal point then make it a double
>                 final Double d = createDouble(str);
>                 if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros)))
{
>                     return d;
>                 }
>             }
>         } catch (final NumberFormatException nfe) { // NOPMD
>             // ignore the bad number
>         }
>         return createBigDecimal(str);
>     }
> {color:#8bd1e2}



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message