Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Quick Java question

  • 21-05-2006 04:14PM
    #1
    Closed Accounts Posts: 25


    Lads

    This is the code for a VERY simple currency converter in Swing. I am after a fast and dirty way to limit the dollarAmount field to 2 decimal places. Is there any way of specifying it in the constructor?
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JPanel;
    
    public class Converter extends JPanel{
    	JButton convert;
    	JTextField euro;
    	JTextField dollar;
    	JTextField rate;
    	JLabel euroLabel;
    	JLabel dollarLabel;
    	JLabel rateLabel;
    	
    	public Converter (){
    		super();
    		euroLabel = new JLabel ("Euro (€): ");
    		dollarLabel = new JLabel ("Dollar ($): ");
    		rateLabel = new JLabel ("Rate: ");
    		
    		euro = new JTextField ("0.00", 8);
    		dollar = new JTextField ("0.00", 8);
    		dollar.setEditable (false);
    		rate = new JTextField ("1.27", 4);
    		
    		convert = new JButton ("CONVERT!");
    		
    		convert.addActionListener (new ActionListener (){
    			public void actionPerformed (ActionEvent ae) {
    				double euroAmount = Double.parseDouble(euro.getText());
    				double r = Double.parseDouble(rate.getText());
    				double dollarAmount = euroAmount*r;
    				dollar.setText(""+ dollarAmount);
    			}
    		});
    		add(euroLabel);
    		add(euro);
    		add(rateLabel);
    		add(rate);
    		add(convert);
    		add(dollarLabel);
    		add(dollar);
    	}
    	public static void main (String args[]){
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(new Converter());
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    
    

    Many thanks in advance...


Comments

  • Closed Accounts Posts: 13 smythmark


    Something like this should do it

    DecimalFormat df = new DecimalFormat("#,###.00");
    double d = 1234.567;
    System.out.println(df.format(d));

    so for example 1234.678 becomes 1,234.57.


  • Registered Users, Registered Users 2 Posts: 58 ✭✭Barry.Commins


    You could use a formatted text field and a decimal formatter.

    You should also use dollar.setValue() instead of dollar.setText() in your ActionListener() if you want to limit the number of digits.

                    //......
                    
                    DecimalFormat df = new DecimalFormat("#,###.##");
                    df.setMinimumFractionDigits(2);
                    df.setMaximumFractionDigits(2);
                    
    		dollar = new JFormattedTextField (df);
    
    

    you will also need to set the number of columns and the the inital value for your formatted text field, but it's pretty straight-forward.

    There are probably easier ways to do it, but this way should work fine.


Advertisement