PDA

View Full Version : help java


MinhHoang
10-11-2002, 05:10 PM
public static void doDouble()
{

double doublein;
String inputin=JOptionPane.showInputDialog("Please enter a double ");
doublein=Double.parseDouble(inputin);

JOptionPane.showMessageDialog(null, "value is " + doublein);

}

User entera double. The function should read the double into a local variable and print it to the screen with 2 places after decimal???

Lam sao lam cho no print out 2 places after decimal?
chi dum,
thanks

openbsd
12-17-2002, 01:24 AM
Do this
import java.text.DecimalFormat;
at the beginning of your program

Declare a variable of type DecimalFormat
DecimalFormat twoDigits = new DecimalFormal("0.00");

Put this in that statement:
JOptionPane.showMessageDialog(null, "value is " + twoDigits.format(doublein) );

In order word, to format your output to appear with 2 numbers after the decimal place, use the format() function of the DecimalDigit class.
For example if your variable name is ABC, then do mytwodigits.format(ABC)

Good luck!