views:

149

answers:

4

Hi,

I've been reading up on the net about the issues with handling float and double types in java. Unfortunately, the image is still not clear. Hence, i'm asking here direct. :(

My MySQL table has various DECIMAL(m,d) columns. The m may range from 5 to 30. d stays a constant at 2.

Question 1.

What equivalent data-type should i be using in Java to work (i.e store, retrieve, and process) with the size of the values in my table? (I've settled with double - hence this post).

Question 2.

While trying to parse a double from a string, i'm getting errors

Double dpu = new Double(dpuField.getText());

for example -

"1" -> java.lang.NumberFormatException: empty String
"10" -> 1.0
"101" -> 10.0
"101." -> 101.0
"101.1" -> 101.0
"101.19" -> 101.1

What am i doing wrong? What is the correct way to convert a string to a double value? And what measures should i take to perform operations on such values?

EDIT

This is the code -

    System.out.println(dpuField.getText());
    Double dpu = new Double(dpuField.getText());
    System.out.println(dpu);

Yes, the problem lies with getText() reporting the wrong value of the dpuField. This method is called on the JTextField keyTyped event. So what's going wrong here?

EDIT 2

Looking at : http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/keylistener.html Apparently, keyTyped() does not give me the keycode. I'll have to switch to keyRealeased()

+4  A: 
  1. if you need exact precision without rounding errors, you should use a BigDecimal.

  2. Your code looks OK - could it be that dpuField.getText() somehow cuts the last character from the string values you list above?

Update: you say

Yes, the problem lies with getText() reporting the wrong value of the dpuField. This method is called on the JTextField keyTyped event.

Could it be that getText() returns the value of the field before the last typed key is actually appended to it?

Péter Török
@wretrOvian, see my update.
Péter Török
but isn't getText() called AFTER the keyTyped event occurs? Meaning that the last typed character is already on the textfield. Puzzling. :@ Anyway, i think the major problem is solved here. :)
wretrOvian
here we go. It becomes clear here http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/keylistener.html
wretrOvian
+1  A: 

For decimal, I believe you risk losing precision if you don't use a BigDecimal on the Java side, as some decimal fractions can't be stored as a binary fraction.

Prefer Double.valueOf(String) over the constructor, but that's a valid way. Something else must be going on (i.e. I doubt those are the actual String values you're passing in).

Mark Peters
+5  A: 

What equivalent data-type should i be using in Java to work (i.e store, retrieve, and process) with the size of the values in my table? (I've settled with double - hence this post).

Since it's a DECIMAL field, you should prefer java.math.BigDecimal. You can store it in DB using PreparedStatement#setBigDecimal() and you can retrieve it from DB using ResultSet#getBigDecimal().

While trying to parse a double from a string, i'm getting errors

This can't be true. The problem lies somewhere else. Maybe it is just not returning the data you expect to be returned or you are not using/debugging the values you expect them to be.

BalusC
you're right. Check the edits.
wretrOvian
Use [`JFormattedTextField`](http://java.sun.com/javase/6/docs/api/javax/swing/JFormattedTextField.html). See [this tutorial](http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html).
BalusC
+1  A: 

Question1: It's bad idea to map DECIMAL columns to Double, usually the BigDecimal is the correct type. http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html#1055175

Question 2: You are doing something wrong; print the String value before converting.

leonbloy