You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@juneau.apache.org by "Peter Haumer (JIRA)" <ji...@apache.org> on 2016/10/07 07:25:20 UTC

[jira] [Commented] (JUNEAU-15) StringUtils.parseNumber(String, Class) returns doubles always as floats

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

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

That solution had its numerical challenges and did not pass all the tests. What about this one, which does not look very elegant, but as this is all about parsing and preserving the parsed strings when mapped to float or double as well as when serializing them back to strings this solution could make sense:

{code}
	if (type == Double.class || type == Double.TYPE) {
		Double d = Double.valueOf(s);
		Float f = Float.valueOf(s);
		if (isAutoDetect && (!isDecimal) && d.toString().equals(f.toString()))
			return f;
		return d;
	}
{code}

I added the test with the numbers that failed the current code returning them as float:
{code}
	assertTrue(isNumeric("0.84370821629078d"));
	assertEquals(0.84370821629078d, parseNumber("0.84370821629078d", null));
{code}


> 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)