You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thomas Neidhart (JIRA)" <ji...@apache.org> on 2012/11/11 13:19:12 UTC

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

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

Thomas Neidhart commented on LANG-693:
--------------------------------------

Why not trying to create a double first in the createNumber method:

{noformat}
                //Must be a float,double,BigDec
                boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
                try {
                    Float f = createFloat(str);
                    if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                        return f;
                    }
                } catch (NumberFormatException nfe) { // NOPMD
                    // ignore the bad number
                }
                try {
                    Double d = createDouble(str);
                    if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                        return d;
                    }
                } catch (NumberFormatException nfe) { // NOPMD
                    // ignore the bad number
                }
{noformat}

Right now, we first try to convert it to a float, and if it does not fit, we try it with double.
But this way we will always lose the precision unless one specifies the 'd' suffix.

The default in java for decimal values is double anyway (without specifying a type qualifier), so it makes sense to use the same behavior in the createNumber method imho.

If somebody really wants to have a float, he/she can specify the 'f' type qualifier.
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira