Yes, in my locale decimal point is a comma.
When the user inputs 12345.467 for example, i just want 12345.46, i want the inputed number truncated with two digits left after decimal point; not rounded. The default behavior with setMaximumFractionDigits() (in java.text.NumberFormat) method rounds every value ==> For Example, with the above number inputed, i.e., 12345.467 , the text field displays 12345.47 if you call setMaximumFractionDigits(2).
I am posting the code i use, if you wish to see...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/*
* Created on 05.May.2004
*/
/**
* @author Barış AKSU
*
* TODO To change the template for this generated type comment go to
* Window - Preferences -
Java - Code Generation - Code and Comments
*/
import java.awt.GridLayout;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;
public class FormatEx1 extends JPanel {
private JFormattedTextField field1;
private JFormattedTextField field2;
public FormatEx1() {
super(new GridLayout(2, 2));
setUpField1();
setUpField2();
displayFields();
}
private void displayFields() {
add(new JLabel("field 1:"));
add(field1);
add(new JLabel("field 2:"));
add(field2);
}
private void setUpField1() {
NumberFormat format1;
format1 = NumberFormat.getInstance();
format1.setMinimumFractionDigits(2);
format1.setMaximumFractionDigits(2);
field1 = new JFormattedTextField(format1);
}
private void setUpField2() {
MaskFormatter format2;
try {
format2 = new MaskFormatter("(###) ###-####");
field2 = new JFormattedTextField(format2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(
String[] args) {
JFrame frame = new JFrame("Number Format Demo");
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.getContentPane().add(new FormatEx1());
frame.setVisible(true);
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^3