juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Peter Haumer (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (JUNEAU-15) StringUtils.parseNumber(String, Class<? extends Number>) returns doubles always as floats
Date Sat, 08 Oct 2016 21:16:20 GMT

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

Peter Haumer commented on JUNEAU-15:
------------------------------------

Hello James.
This sounds tricker unless there a standard JDK function to normalize and count the significant
digits? (Could not find one.) 12345.678 or 1.2345678e4f is still a valid float, though; so
this is the most pessimistic approach, right?

What I like about my solution is that it seems the right heuristic for the scenarios that
I am interested in with Juno: I receive json with numbers, parse them, use them for computations
and then serialize them again to send them on. Comparing that the .toString() results are
the same and its internal rounding leads back to the same float when I serialize, and if in
doubt falls back to double supports my use cases well.


> StringUtils.parseNumber(String, Class<? extends Number>) returns doubles always
as floats
> -----------------------------------------------------------------------------------------
>
>                 Key: JUNEAU-15
>                 URL: https://issues.apache.org/jira/browse/JUNEAU-15
>             Project: Juneau
>          Issue Type: Bug
>          Components: Code
>            Reporter: Peter Haumer
>
> When you serialize a bean with a member of type "double" and read it into an ObjectMap
the map will show the value now only with float precision. 
> The reason is that the parsing calls org.apache.juneau.internal.StringUtils.parseNumber(String,
Class<? extends Number>), which in Line 148 tests for float by "d >= -Float.MAX_VALUE
&& d <= Float.MAX_VALUE", but does not check if the precision required for the
string fits into the float. What about doing the test like this:
> {code}
> 	if (type == Double.class || type == Double.TYPE) {
> 		Double d = Double.valueOf(s);
> 		Float f = d.floatValue();
> 		if (isAutoDetect && (!isDecimal) && d.compareTo(new Double(f)) == 0)
> 			return f;
> 		return d;
> 	}
> {code}



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

Mime
View raw message