Results 1 to 10 of 10
- 07-05-2010, 01:54 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
JSpinner getValue method bug in double?
Hello,
I am using a JSpinner with a double model.
When retreiving the values with the getValue method some values are retreived incorrectly. For example.
If in the JSpinner the value is 0.3, the getValue method retreives 0.300000000004... If it is 0.8 the value is 0.79999999999999 or with 1.9 is 1.900000000000000001
Some values (0.2, 0.3, 0.4) are retreived correctly...
What the heck is happening?
-
The program is working correctly. You will need to adjust your understanding though on how computers deal with floating point numbers. Please look at this article for a start:
what every computer scientist should know about floating point numbers
After digesting this, you may wish to use a DecimalFormat object to help make your number look pretty.
- 07-05-2010, 03:50 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Uhhh ohhh...
Modifying the type value to decimal means to change a bit of things (database field, JSpinners, several variables that made use of it...)
Is there a trick to just display the "pretty" value of the double but in reality, deal with the double value?
-
No one said anything about changing a type of the values held by the JSpinner, please re-read the above. Rather I recommended that you format the returned value and you do this only when you want to display it as a String . Here's where the DecimalFormat class will help.
- 07-05-2010, 04:03 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
There is not a decimal model for a JSpinner, anyway I just dont understand if I am just asking my program to display the value of a JSpinner, and if in the JSpinner is 0.1 marked
Last edited by cotarelo; 07-05-2010 at 04:05 PM.
-
Again, I'm not recommending that you change anything about JSpinner itself. I think we're both getting frustrated here due to mutual misunderstanding, and perhaps the best way to show you what I mean is programmatically. I'm going to ask you to create a very small GUI program is compilable and that has a JSpinner and that demonstrates your problem. Then perhaps we can show you how to fix it if possible.
-
Heck, I created one myself. Please have a look at this:
Java Code:import java.text.DecimalFormat; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class SpinnerDoubleTrouble { private static void createAndShowUI() { final DecimalFormat dFormat = new DecimalFormat("0.00"); final JTextField nonFormattedField = new JTextField(15); final JTextField formattedField = new JTextField(15); Double value = new Double(0.0); Double min = new Double(-5.0); Double max = new Double(5.0); Double step = new Double(0.1); final SpinnerNumberModel numberModel = new SpinnerNumberModel(value, min, max, step); final JSpinner spinner = new JSpinner(numberModel); spinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { double value = ((Double)spinner.getValue()).doubleValue(); String unformattedValue = String.valueOf(value); String formattedValue = dFormat.format(value); nonFormattedField.setText(unformattedValue); formattedField.setText(formattedValue); } }); JPanel panel = new JPanel(); panel.add(spinner); panel.add(Box.createHorizontalStrut(25)); panel.add(new JLabel("Unformatted:")); panel.add(nonFormattedField); panel.add(Box.createHorizontalStrut(25)); panel.add(new JLabel("Formatted:")); panel.add(formattedField); JFrame frame = new JFrame("SpinnerDoubleTrouble"); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
-
The key to the code above is the ChangeListener:
Java Code:spinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { // first get the value from the JSpinner double value = ((Double)spinner.getValue()).doubleValue(); // take the raw value and put it not a String String unformattedValue = String.valueOf(value); // or format it using the DecimalFormat("0.00") created above String formattedValue = dFormat.format(value); // place respective Strings into appropriate JTextFields nonFormattedField.setText(unformattedValue); formattedField.setText(formattedValue); } });
- 07-05-2010, 05:14 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 43
- Rep Power
- 0
Thank you very much! I see now your point :)
-
You're welcome, but if you haven't done so, please read the article linked to in my first post so you can gain a deeper understanding of this common occurrence.
Similar Threads
-
JSpinner resizes, I don't want that.
By JosAH in forum AWT / SwingReplies: 4Last Post: 05-30-2010, 09:11 AM -
Jtable Jspinner help
By chiragkini in forum AWT / SwingReplies: 5Last Post: 08-15-2009, 04:36 AM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
convert getValue result in a 4 digit number
By roseline43 in forum New To JavaReplies: 0Last Post: 09-02-2008, 08:44 PM -
How to getvalue from properties file into JSP
By jaga in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-04-2008, 08:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks