0

I have developed a Swing application. When I try to load data, one numeric text field shows 1000000000.00 (which is correct. I will call this field as first field) where as another numeric text field shows 99999997952.00 instead of (100000000000.00. I will call this field as second field).

Also, after loading the data, when I move the cursor on the first field the number remains the same. Now if I take the cursor to the second field and then click on the first field the value changes to 999999979.52.

Both the numeric text fields are DecimalFormat. I create those fields in the following way:

DecimalFormat format = new DecimalFormat("##,###,#######");
NumericTextField field1= new NumericTextField(10,format);

Also I display the fields in the following way:

DecimalFormat formatter = new DecimalFormat("0.00");
field1.setText(formatter .format(new Double(field1Value)))

Can anyone please help me to understand:

  1. Why the second field shows 99999997952.00 instead of 100000000000.00
  2. Why the first field changes its value to 999999979.52
chiwangc
  • 3,566
  • 16
  • 26
  • 32
  • And you expect us to answer without seeing any piece of code ? You probably superimposed two text fields at the same location but it won't be confirmed without your code. – Dici Aug 31 '14 at 14:13
  • 2
    @Dici: yeah, we can answer it. It's because the OP will need to gain an understanding of the limits of using floating point numbers on a digital computer. He will eventually understand that this has nothing to do with Swing or even Java but rather the limits placed by the general IEEE representation of floating point numbers. Solution: use BigDecimal. – Hovercraft Full Of Eels Aug 31 '14 at 14:16
  • @Dici: but you're right, we can't see the bug in his code that causes the 2nd field's text to change. For that he should consider creating and posting a [minimal example program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Aug 31 '14 at 14:49
  • I had not thought about the number of decimals (which seems relevant to me now), but I do remember having had similar problems due to misplaced fields (one on the top of the other). – Dici Aug 31 '14 at 14:54

0 Answers0