简体   繁体   English

Java:双数据类型十进制格式逗号问题

[英]Java: Double Datatype Decimal format commas issue

I'm solving a problem with math.pow function when the answer appear on text field with any enter value such as 10.It will be appear in commas.I use a decimal format library but nothing happen.Is there a different way to do that.I want answer like this 我正在解决math.pow函数的问题,当答案出现在文本字段上时,任何输入值如10。它将出现在逗号中。我使用小数格式库但没有任何反应。有不同的方法可以做那我想要这样回答

10000000000
Not
10,000,000,000

Code: 码:

    public class Gra extends JFrame{
    private JTextField textField;
    private JTextField textField_1;

    DecimalFormat d = new DecimalFormat("");
    public Gra(){
        super("Frame");
        getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setBounds(163, 206, 122, 28);
        getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(163, 106, 122, 28);
        getContentPane().add(textField_1);
        textField_1.setColumns(10);

        JLabel lblEnterValue = new JLabel("Enter Value");
        lblEnterValue.setBounds(183, 89, 69, 16);
        getContentPane().add(lblEnterValue);

        JLabel lblAnswer = new JLabel("Answer");
        lblAnswer.setBounds(195, 187, 55, 16);
        getContentPane().add(lblAnswer);

        JButton btnClick = new JButton("Click");
        btnClick.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                double num,ans;
            String w=   textField_1.getText();
            num = Double.parseDouble(w);
            ans=Math.pow(num, 10);
            textField.setText(""+d.format(ans));


            }
        });
        btnClick.setBounds(183, 249, 90, 28);
        getContentPane().add(btnClick);


    }
}

Main 主要

public class Main {

    public static void main(String[] args) {
        Gra obj=new Gra();
        obj.setSize(450, 400);
        obj.setResizable(false);

        obj.setDefaultCloseOperation(obj.EXIT_ON_CLOSE);
        obj.setLocationRelativeTo(null);
        obj.setVisible(true);
    }

}
d.setGroupingUsed(false);

会解决你的问题。

Hope this would be of some help 希望这会有所帮助

import java.text.*;
class Test {
        public static void main(String... args) {
                String  p = "10000000000";
                Double amount = Double.parseDouble(p);
                DecimalFormat df = new DecimalFormat("##,###,###,###");
                System.out.println(df.format(amount));
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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