Results 1 to 4 of 4
  1. #1
    bioa10 is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default 0.01 doesnt add correctly

    So idk if this is the right place for this but I have a JButton that when pressed it adds 0.01 to a double variable, but the tenth time it is pressed it goes to 0.0999999999999 and so on

    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    public class CoinCountMain implements ActionListener{
    	JButton onec = null;
    	JButton fivec = null;
    	JButton tenc = null;
    	JButton tfc = null;
    	JButton oned = null;
    	JButton twod = null;
    	JButton fived = null;
    	JButton tend = null;
    	JButton twentyd = null;
    	JButton total = null;
    	JButton clear = null;
    	double a = 0;
    	public void actionPerformed(ActionEvent event){
    		Object control = event.getSource();
    		if(control == onec){
    			a = a+0.01;
    		} else if(control == fivec){
    			a = a+0.05;
    		}else if(control == tenc){
    			a = a+0.10;
    		}else if(control == tfc){
    			a = a+0.25;
    		}else if(control == oned){
    			a = a+1;
    		}else if(control == twod){
    			a = a+2;
    		}else if(control == fived){
    			a = a+5;
    		}else if(control == tend){
    			a = a+10;
    		}else if(control == twentyd){
    			a = a+20;
    		}else if(control == total){
    			JOptionPane.showMessageDialog(null, "You have $" + a);
    		}else if(control == clear){
    			a = 0;
    		}
    	}
    	public static void main(String[] args){
    		new CoinCountMain();
    	}
    	public CoinCountMain(){
    		FlowLayout myLayout = new FlowLayout();
    		JFrame myFrame = new JFrame();
    		myFrame.setTitle("Coin Counter");
    		onec = new JButton("$0.01");
    		onec.addActionListener(this);
    		fivec = new JButton("$0.05");
    		fivec.addActionListener(this);
    		tenc = new JButton("$0.10");
    		tenc.addActionListener(this);
    		tfc = new JButton("$0.25");
    		tfc.addActionListener(this);
    		oned = new JButton("$1.00");
    		oned.addActionListener(this);
    		twod = new JButton("$2.00");
    		twod.addActionListener(this);
    		fived = new JButton("$5.00");
    		fived.addActionListener(this);
    		tend = new JButton("$10.00");
    		tend.addActionListener(this);
    		twentyd = new JButton("$20.00");
    		twentyd.addActionListener(this);
    		total = new JButton("Total");
    		total.addActionListener(this);
    		clear = new JButton("clear");
    		clear.addActionListener(this);
    		myFrame.add(onec);
    		myFrame.add(fivec);
    		myFrame.add(tenc);
    		myFrame.add(tfc);
    		myFrame.add(oned);
    		myFrame.add(twod);
    		myFrame.add(fived);
    		myFrame.add(tend);
    		myFrame.add(twentyd);
    		myFrame.add(total);
    		myFrame.add(clear);
    		myFrame.setLayout(myLayout);
    		myFrame.pack();
    		myFrame.setLocation(200, 300);
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		myFrame.setVisible(true);
    	}
    }
    Also say if i click the one cent button 6 times too fast then it might show this: 0.06000000005 idk the exact number of zeros that was just an example

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: 0.01 doesnt add correctly

    Welcome to the joys of floating point math. Due to the internal representation of floating point numbers you must take care how you perform mathematical operations on them. I suggest you either use integers or longs and do your calculations in cents and then convert to dollars (assuming US currency). Or use BigDecimal. This topic has been covered many times in this forum. There is an article which discusses it on the web. You can either search the forum for the link or someone may post it for you.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: 0.01 doesnt add correctly

    Nothing to do with AWT/Swing. Moving to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: 0.01 doesnt add correctly

    Use DecimalFormat class to control precision. java.text.DecimalFormat
    Last edited by allaudin; 08-21-2013 at 08:24 AM.

Similar Threads

  1. HttpURLConnection doesnt quite cut it
    By chopficaro in forum Advanced Java
    Replies: 1
    Last Post: 08-23-2012, 10:16 PM
  2. it doesnt see my 'if' statements
    By bilsch01 in forum New To Java
    Replies: 4
    Last Post: 06-27-2012, 02:43 PM
  3. Applet doesnt start or it doesnt show
    By 3dprogger in forum Java Applets
    Replies: 2
    Last Post: 01-07-2011, 08:03 PM
  4. Replies: 7
    Last Post: 09-28-2010, 01:17 AM
  5. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 03:50 PM

Posting Permissions

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