You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2016/05/24 09:29:12 UTC

[jira] [Commented] (LANG-1018) NumberUtils.createNumber(final String str) Precision will be lost

    [ https://issues.apache.org/jira/browse/LANG-1018?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15297957#comment-15297957 ] 

ASF GitHub Bot commented on LANG-1018:
--------------------------------------

GitHub user NickManley opened a pull request:

    https://github.com/apache/commons-lang/pull/156

    LANG-1018: Fix precision loss on NumberUtils.createNumber(String)

    **Warning: This patch will break backwards compatibility.**
    
    This patch is designed to fix the loss of precision when using NumberUtils.createNumber(String). However, the fix is not 100% backwards compatible with the currently released commons-lang 3.4. To fix the loss of precision, values that were previously returning Floats are now returning Doubles. Some explicit casts may break as a result.
    
    ```java
    // Works in 3.4, but causes ClassCastException in patch.
    Float f = (Float) NumberUtils.createNumber("-160952.54");
    System.out.println(f); // prints "-160952.55"
    
    // Works with patch.
    Double d = (Double) NumberUtils.createNumber("-160952.54");
    System.out.println(d); // prints "-160952.54"
    ```

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/NickManley/commons-lang LANG-1018

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/commons-lang/pull/156.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #156
    
----
commit b77db22614da6f0c674b57cd0086048b58b30135
Author: Nick Manley <ni...@outlook.com>
Date:   2016-05-24T09:10:29Z

    LANG-1018: Fix precision loss on NumberUtils.createNumber(String)

----


> 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
>            Assignee: Duncan Jones
>             Fix For: Patch Needed
>
>
> 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:
> {code:java}
> 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);
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)