Results 1 to 10 of 10
  1. #1
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    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 05:05 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cotarelo View Post
    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
    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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();
             }
          });
       }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);
             }
          });

  9. #9
    cotarelo is offline Member
    Join Date
    Mar 2010
    Posts
    43
    Rep Power
    0

    Default

    Thank you very much! I see now your point :)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JSpinner resizes, I don't want that.
    By JosAH in forum AWT / Swing
    Replies: 4
    Last Post: 05-30-2010, 10:11 AM
  2. Jtable Jspinner help
    By chiragkini in forum AWT / Swing
    Replies: 5
    Last Post: 08-15-2009, 05:36 AM
  3. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  4. convert getValue result in a 4 digit number
    By roseline43 in forum New To Java
    Replies: 0
    Last Post: 09-02-2008, 09:44 PM
  5. How to getvalue from properties file into JSP
    By jaga in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-04-2008, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •