Paul Newport
Oct 26th, 2006, 06:16 AM
I have a major problem in an application, in that if I have a float on an object, and am mapping between a jsp field (i.e. a String) and a float, I am getting conversion errors if the screen field is 9 digits or larger.
Example:
I enter 333333333, navigate to another page, come back, it shows the field I entered as 333333344.
An easy to run test case demonstrates this (see end of post).
I have debugged the Spring code and what is happening is that in org.springframework.util.NumberUtils it is executing this piece of code:
else if (targetClass.equals(Float.class)) {
return new Float(number.floatValue());
}
I can see that number is a Long, with a value of 333333333. However number.floatValue() returns 333333344 which is completely wrong.
The bottom line is that Spring MVC doesn't seem to work properly when mapping jsp fields to floats and back again.
Can someone double check my test and see if I am doing anything wrong ?
import java.text.DecimalFormat;
import java.text.NumberFormat;
import junit.framework.TestCase;
import org.springframework.beans.propertyeditors.CustomNu mberEditor;
public class TestCorruptFloat extends TestCase {
NumberFormat numberFormat;
String expected = "333333333";
public void setUp() {
// number format editor
numberFormat = new DecimalFormat("0.00");
numberFormat.setMinimumFractionDigits(2);
}
public void testConvert() {
CustomNumberEditor customNumberEditor = new CustomNumberEditor(
Float.class, numberFormat, true);
customNumberEditor.setAsText(expected);
assertTrue(((Float)(customNumberEditor.getValue()) ).floatValue()==333333333);
String actual = customNumberEditor.getAsText();
System.out.println(expected + " " + actual);
assertEquals(expected, actual);
}
}
Example:
I enter 333333333, navigate to another page, come back, it shows the field I entered as 333333344.
An easy to run test case demonstrates this (see end of post).
I have debugged the Spring code and what is happening is that in org.springframework.util.NumberUtils it is executing this piece of code:
else if (targetClass.equals(Float.class)) {
return new Float(number.floatValue());
}
I can see that number is a Long, with a value of 333333333. However number.floatValue() returns 333333344 which is completely wrong.
The bottom line is that Spring MVC doesn't seem to work properly when mapping jsp fields to floats and back again.
Can someone double check my test and see if I am doing anything wrong ?
import java.text.DecimalFormat;
import java.text.NumberFormat;
import junit.framework.TestCase;
import org.springframework.beans.propertyeditors.CustomNu mberEditor;
public class TestCorruptFloat extends TestCase {
NumberFormat numberFormat;
String expected = "333333333";
public void setUp() {
// number format editor
numberFormat = new DecimalFormat("0.00");
numberFormat.setMinimumFractionDigits(2);
}
public void testConvert() {
CustomNumberEditor customNumberEditor = new CustomNumberEditor(
Float.class, numberFormat, true);
customNumberEditor.setAsText(expected);
assertTrue(((Float)(customNumberEditor.getValue()) ).floatValue()==333333333);
String actual = customNumberEditor.getAsText();
System.out.println(expected + " " + actual);
assertEquals(expected, actual);
}
}