0

I want to make sure that I understand how to change characters (mainly "." into "," and vice versa).

So I wrote a program, but I can't type in a decimal number. What should I do?

import java.text.DecimalFormat;
import javax.swing.*;

public class decimalFormat {
    public static void main(String[]args) {
        DecimalFormat df= new DecimalFormat("##,##0.00");
        String s= JOptionPane.showInputDialog(null,"Please enter a number");
        s= df.format(s);
        double t=Double.parseDouble(s);
        JOptionPane.showMessageDialog(null,"Your number rounded to two digits is");
    }
}
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
markk
  • 1
  • 1

1 Answers1

0

You should use DecimalFormat.parse() instead of DecimalFormat.format(). parse() is used to convert a String to a Number, format() is used to convert a Number to a String.

From the docs:

parse(): Parses text from the beginning of the given string to produce a number.

format(): Formats an object to produce a string.

Your code should look like this:

DecimalFormat df= new DecimalFormat("##,##0.00");
String s = JOptionPane.showInputDialog(null,"Please enter a number");
double t = df.parse(s).doubleValue();
Community
  • 1
  • 1
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56