开发者

How to remove comma from TextField in Java

开发者 https://www.devze.com 2023-01-11 10:16 出处:网络
I have a JFormattedTextField with the Name Hectare. The double type value is declared as shown below String cultivationSize = JFormattedTextField3.getText();

I have a JFormattedTextField with the Name Hectare. The double type value is declared as shown below

         String cultivationSize = JFormattedTextField3.getText();
         double hectare = Double.parseDouble(cultivationSize);

Now the problem is that when i enter more than 3 digits, by default the comma is entered after 3 digits, e.g. 1,000. I have to add this value to some other value. But, due to this comma,I am unable to do it.

How can I remove comma and add this value to some开发者_开发技巧 other value?


Call the getValue() instead of getText() on JFormattedTextField


A much easier solution

Format format = NumberFormat.getIntegerInstance();
format.setGroupingUsed(false);
JFormattedTextField jtf = new JFormattedTextField(format);

This will remove the grouping of the numbers using comma.


You should use MaskFormater like this:

zipField = new JFormattedTextField(
                    createFormatter("#####"));
...
protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter(s);
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return formatter;
}


use string.replace(",","");

i.e. your code should look like -

double hectare = Double.parseDouble(cultivationSize.replaceAll(",",""));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号