简体   繁体   中英

How do I get a decimal separator?

I'm creating a calculator and want to have a button that separates numbers so that I get decimal numbers. I'm new to this and have been thinking and searching for one week without progression. I've read about NumberFormat and DecimalFormat, but I can't get them to work, and I frankly don't know if these are right at all.

import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.*;

/** skapar en enkel räknare med det vanligaste räknesätten 
*Java Doc kommentarer för utförliga kommentarer för en längre applikation*/
public class CounterApplication extends JFrame implements ActionListener
{
private JTextField t1;
private JButton b1;
private JButton b2;
private JButton bIndex[] = new JButton[24];
private double tal = 0;
private double talTemp = 0;
private String sign;
private JPanel p1;
private JPanel p2;
private JPanel p3;

/** initierar Räknaren som en applet */

public CounterApplication()
{
    setLayout(new BorderLayout());
    setBackground(Color.lightGray);
    p1 = new JPanel();
    p1.setLayout(new FlowLayout());
    p1.add(t1 = new JTextField(22));
    t1.setFont(new Font("Sans Serif", Font.BOLD, 12));
    t1.setEnabled(false);
    t1.setText(""+tal);

    p2 = new JPanel();
    p2.setLayout(new GridLayout(4,6,4,4));
    for(int i = 0; i<24; i++)
    {
        bIndex[i] = new JButton();
        bIndex[i].addActionListener(this);
        bIndex[i].setFont(new Font("Sans Serif", Font.BOLD, 16));
        bIndex[i].setForeground(Color.blue);
        p2.add(bIndex[i]);
    }
    bIndex[0].setLabel("7");
    bIndex[1].setLabel("8");
    bIndex[2].setLabel("9");
    bIndex[3].setForeground(Color.red);
    bIndex[3].setLabel("/");
    bIndex[4].setLabel("sqrt");
    bIndex[5].setLabel("sin");
    bIndex[6].setLabel("4");
    bIndex[7].setLabel("5");
    bIndex[8].setLabel("6");
    bIndex[9].setForeground(Color.red);
    bIndex[9].setLabel("*");
    bIndex[10].setLabel("%");
    bIndex[11].setLabel("cos");
    bIndex[12].setLabel("1");
    bIndex[13].setLabel("2");
    bIndex[14].setLabel("3");
    bIndex[15].setForeground(Color.red);    
    bIndex[15].setLabel("-");
    bIndex[16].setLabel("1/x");
    bIndex[17].setLabel("log");
    bIndex[18].setLabel("0");
    bIndex[19].setLabel("+/-");
    bIndex[20].setLabel(",");
    bIndex[21].setForeground(Color.red);
    bIndex[21].setLabel("+");
    bIndex[22].setLabel("=");
    bIndex[23].setLabel("ln");


    p3 = new JPanel();
    p3.setLayout(new GridLayout(1,2,4,4));
    b1 = new JButton("CE");
    b1.setFont(new Font("Sans Serif", Font.BOLD, 16));
    b1.setForeground(Color.red);
    p3.add(b1,"South");
    b2 = new JButton("C");
    b2.setFont(new Font("Sans Serif", Font.BOLD, 16));
    b2.setForeground(Color.red);
    p3.add(b2,"South");
    b1.addActionListener(this);
    b2.addActionListener(this);
    add(p1,"North");
    add(p2,"Center");
    add(p3,"South");
    setVisible(true);
    pack();
        }

    /** styr händelsehanteringen i räknaren */

    public void actionPerformed(ActionEvent e)
    {


        if(e.getSource() == bIndex[20])        //Knappen ","
        {

        }

        if(e.getSource() == bIndex[21])        //Knappen "+"
        {
            count();
            sign = "+";
            t1.setText("" + talTemp);
        }

        if(e.getSource() == bIndex[22])        //Knappen "="
        {
            count();
            tal = talTemp;
            sign = " ";
            t1.setText("" + talTemp);
        }

         if(e.getSource() == bIndex[23])        //Knappen "ln"
        {
            /*count();
            sign = "ln";*/
            talTemp = tal;
            tal = Math.log(talTemp);
            t1.setText("" + tal);
        }

        if(e.getSource() == b1)                //Knappen CE
        {
            tal = 0;
            t1.setText("" + tal);
        }


        if(e.getSource() == b2)                    //Knappen C
        {    
            tal = 0;
            talTemp= 0;
            t1.setText(" " + tal);
        }

    }    
    /** beräknar resultatet av den matematiska operationen */

    public void count()
    {
        if(talTemp>0){
            if(sign == "+")
                tal = talTemp + tal;
            if(sign == "-")
                tal = talTemp - tal;
            if(sign == "*")
                tal = talTemp * tal;
            if(sign == "/")
                tal = talTemp / tal;
            if(sign == "%")
                tal = talTemp * 0.01;
            if(sign == "sqrt")
                tal = Math.sqrt(talTemp);
            if(sign == "1/x")
                tal = 1 /talTemp;
            if(sign == "=")
                tal = talTemp + tal;
            if(sign == "sin")
                tal = Math.sin(talTemp);
            if(sign == "cos")
                tal = Math.cos(talTemp);
            if(sign == "log")
                tal = Math.log(talTemp);    
        }
        talTemp = tal;
        tal = 0;
    }

    public static void main(String args[])
    {
        CounterApplication c = new CounterApplication();
    }
}

If you make the separator button insert a space in the user input string, you could then call userInput.split(" ") which would give you a String[] array of all the individual numbers typed in.

String userInput = "100 200 300";
String[] numberStrings = userInput.split(" ");
List<BigDecimal> numbers = Stream.of(numberStrings).map(BigDecimal::new).collect(Collectors.toList());

System.out.println(numbers);    // prints [100, 200, 300]

Old Answer:

You could take user input as a String , perhaps using java.util.Scanner , allowing decimal points. Then use the string in the constructor of BigDecimal . Try this:

String userInput = "1234.5678";
BigDecimal number = new BigDecimal(userInput);
System.out.println(number);    // prints '1234.5678'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM