4 Replies - 650 Views - Last Post: 22 September 2011 - 01:13 AM Rate Topic: -----

#1 cassiopeia  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 86
  • Joined: 03-April 11

Incorrect DecimalFormat Result

Posted 21 September 2011 - 07:09 AM

im aware about many similar problem and DecimalFormat in this forum. i already read them, including source from outside : DecimalFormat 1 and DecimalFormat 2. so please dont shoot the poster for posting similar post :2guns:

basically i use outside source above as reference for my code. but somehow it does not produce result correctly in JFormatterTextField. if i type from blank field, after i press Enter it will produce blank result only when there are 6 numeric char i.e. :

7000 -> 7.000
700000 -> 700.000
7000000 -> (blank)

and if in the field already has value i.e. 7.000, and i put number after that (without modification from beginning) i.e. 7.00000, will produce result only 7 in JFormatterTextField after i press enter.

this my declareation in constructor.
jtPurchasePrice is JFormattedTextField
NumericDocument can be found here

jtPurchasePrice.setDocument(new NumericDocument(2,true));
jtPurchasePrice.addKeyListener(new NumericFormatListener(jtPurchasePrice,"CurrencyInstance"));



and NumericFormatListener :

public class NumericFormatListener implements KeyListener{
    
    private JFormattedTextField jFormattedTextField;
    private NumberFormat numberFormat;
    private String stringNumber;
    private DecimalFormat decimalFormat;
    double doubleNumber;
    private Locale locale;
    
    public NumericFormatListener (JFormattedTextField jFormattedTextField, String option){
        this.jFormattedTextField=jFormattedTextField;      
        locale= Locale.getDefault();
    }        

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==10){
            numberFormat = NumberFormat.getCurrencyInstance(locale);
            decimalFormat = (DecimalFormat)numberFormat;
            decimalFormat.applyPattern("###,###,###.##");
            if (!"".equals(jFormattedTextField.getText())){
                doubleNumber = Double.parseDouble(jFormattedTextField.getText());
                stringNumber = decimalFormat.format(doubleNumber);
                jFormattedTextField.setColumns(10);
                jFormattedTextField.setText(stringNumber);
            }   
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
}



i welcome any hint, idea, link, or whatever how to solve my problem or how to make the code better :)

This post has been edited by cassiopeia: 21 September 2011 - 07:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Incorrect DecimalFormat Result

#2 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Incorrect DecimalFormat Result

Posted 21 September 2011 - 09:20 AM

And what is NumericDocument ? A new class introduced with JRE 1.7 ?
Was This Post Helpful? 0
  • +
  • -

#3 cassiopeia  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 86
  • Joined: 03-April 11

Re: Incorrect DecimalFormat Result

Posted 21 September 2011 - 09:42 AM

View Postcmpshr, on 21 September 2011 - 09:20 AM, said:

And what is NumericDocument ? A new class introduced with JRE 1.7 ?


no, its a class that i got from this forum. i already put the link above.

i think that class does not has any relation for problem i got since the purpose of that class is only to limit (only numeric) input from keyboard.

i think the problem is in KeyPress, but do not know what it is.

This post has been edited by cassiopeia: 21 September 2011 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Incorrect DecimalFormat Result

Posted 21 September 2011 - 12:36 PM

Sure that you can display text using: 123,456,123
but you cannot parse it using Double.parseDouble() which ignore the , character

So if the user enters:
1234<Enter>
you will reformat it into
1,234
then if the user enter 56<enter> you will have
1,23456
and it will fail here

           if (!"".equals(jFormattedTextField.getText())){
                doubleNumber = Double.parseDouble(jFormattedTextField.getText());



May be you can try:
doubleNumber = Double.parseDouble(jFormattedTextField.getText().replace(",","");
Was This Post Helpful? 1
  • +
  • -

#5 cassiopeia  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 86
  • Joined: 03-April 11

Re: Incorrect DecimalFormat Result

Posted 22 September 2011 - 01:13 AM

View Postcmpshr, on 21 September 2011 - 12:36 PM, said:

May be you can try:
doubleNumber = Double.parseDouble(jFormattedTextField.getText().replace(",","");


you are absolutely right about that. thanx mate, very helpful. :^:

mind helping me with the first problem, its about when i type 7 numeric char '1234567' then i press enter, the JFormattedTextField will go blank. and when i see the value in debug mode, this line :

stringNumber = decimalFormat.format(doubleNumber);
jFormattedTextField.setColumns(10);
jFormattedTextField.setText(stringNumber);



stringNumber already has the right value '1.234.567', and then i 'step over' until the third line "jFormattedTextField.setText(stringNumber);" and then i hear beeep sound, 'step over' after that and netbeans lead me to file AWTEventMulticaster.java'

i still try to figure out what is wrong here, and almost run out the idea. if you have something suspicious bout my code please do tell me so i can start over to search the problem again. and thank you for solving my other problem :)

This post has been edited by cassiopeia: 22 September 2011 - 01:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1