Menu

#11 Incorrect conversion of large double to Integer

open
nobody
None
5
2010-03-29
2010-03-29
Anonymous
No

AbstractIntegerMorpher.getIntegerValue converts the supplied object to a string and then truncates at the decimal point to return the integer part. For larger double values, Java uses a scientific-format string like "1.23E8" which then gets incorrectly split. It looks like the primitive IntMorpher class correctly checks if the passed object is already a Number instance first. Helpfully, the standard String format of large *integers* does not use scientific notation, so this bug only manifests itself when converting from double to integer.

To reproduce:

double d = 123456789d;
System.out.println(d);
Object i = MORPHER.morph(Integer.class, d);
System.out.println(i);

Expected output:

1.23456789E8
123456789

Actual output:

1.23456789E8
1

Discussion


Log in to post a comment.

MongoDB Logo MongoDB