2015-06-02 22 views
5

Tôi đang giải quyết vấn đề với hàm math.pow khi câu trả lời xuất hiện trên trường văn bản với bất kỳ giá trị nhập nào như 10.It sẽ xuất hiện trong dấu phẩy.I sử dụng thư viện định dạng thập phân nhưng không happen.Is có một cách khác để làm that.I muốn trả lời như thế này
Java: Double Datatype Định dạng thập phân dấu phẩy

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


    } 
} 

chính

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

} 
+0

Cố gắng đào sâu vào java.text.DecimalFormat với Locale thích hợp. – sakthisundar

Trả lời

2
d.setGroupingUsed(false); 

sẽ giải quyết vấn đề của bạn.

0

Hy vọng điều này sẽ giúp đỡ một số

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)); 
     } 
} 
Các vấn đề liên quan