programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

setMaximumFractionDigits : Digits After Decimal Point without Rounding

savas karabuz
Greenhorn
Posts: 16
Hi everybody,
I have the following lines of code :

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
private JFormattedTextField field1;
...
NumberFormat format1 = NumberFormat.getInstance();
format1.setMinimumFractionDigits(2);
format1.setMaximumFractionDigits(2);
field1 = new JFormattedTextField(format1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When the user inputs into this field, say 12345, it is converted to 12.345,00 after navigating to the next field(Locale specific). But if 12345,467 is inputed, it is converted to 12.345,47.
What i need, and don't know how to get, is just the "two" digits after the decimal point displayed on the screen, not the ones after the rounding occurs.
Alternatively, how can i create a MaskFormatter that accepts any number of digits 'before' the decimal point, but just two after it?
Any bit of help is appreciated...
Thanks in advance...

Eddie Vanda
Ranch Hand
Posts: 283
Originally posted by savas karabuz:
When the user inputs into this field, say 12345, it is converted to 12.345,00 after navigating to the next field(Locale specific). But if 12345,467 is inputed, it is converted to 12.345,47.
What i need, and don't know how to get, is just the "two" digits after the decimal point displayed on the screen, not the ones after the rounding occurs.

I am trying to understand your question.
If 12345,467 is inputed, you want 12.345,46 ?
I take it that in your locale the decimal point is a comma?

savas karabuz
Greenhorn
Posts: 16
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