[
https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13495379#comment-13495379
]
Sebb commented on LANG-693:
---------------------------
As pointed out in the original issue description, the same problem applies to
double/BigDecimal.
If the code is changed to try double first, then the method will never create a
BigDecimal unless it is larger than Double.MAX_VALUE, thus potentially losing
precision again.
As I see it, the problem is that the method currently only switches output type
based on magnitude, not on precision.
Would it make sense to check the precision of the input number and use that to
choose the starting conversion?
e.g. if more than 6 digits are present, use double; if more than 17 digits, use
BigDecimal.
This should be fairly cheap to do, and it would be easy to determine in advance
what number type would be created.
It would not always choose the minimal representation, but at least it would
never lose precision.
[AFAICT:
6 is the max number of digits in a number guaranteed not to lose precision
(some 7/8 digit numbers are also OK).
Similarly, double can support 15 digits guaranteed (to 17 max).]
> 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