currency formatting in a MaskFormater

Discussion in 'Java' started by CD1, Jan 22, 2005.

  1. CD1

    CD1 Guest

    hi guys!

    i wanna create a JFormattedTextField that filters only currency values.
    i did something like:

    JFormattedTextField ftfField = new
    JFormattedTextField(NumberFormat.getCurrencyInstance(myLocale));

    but, i noticed that i have to type the currency symbol on the field!
    but i dont want it! that means, using the default locale, if i wanna
    type 25 dollars i HAVE to type "$25"!!! but i want it to be only "25".
    my locale is another one (brazilian), but the problem is the same.

    i've searched about this on this group, but i havent found a specific
    doubt like mine. there were many topics about formatting string (as a
    variable), but i wanna something to put on a JFormattedTextField (or
    whatever component is better).

    thank u all,
    Cristian Deives

    E-mail: "cristian deives * gmail!computer".replace(" ",
    "").replace("*", "@").replace("!", ".").replace("puter", "")
     
    CD1, Jan 22, 2005
    #1
    1. Advertising

  2. CD1

    Roland Guest

    On 22-1-2005 1:08, CD1 wrote:

    > hi guys!
    >
    > i wanna create a JFormattedTextField that filters only currency values.
    > i did something like:
    >
    > JFormattedTextField ftfField = new
    > JFormattedTextField(NumberFormat.getCurrencyInstance(myLocale));
    >
    > but, i noticed that i have to type the currency symbol on the field!
    > but i dont want it! that means, using the default locale, if i wanna
    > type 25 dollars i HAVE to type "$25"!!! but i want it to be only "25".
    > my locale is another one (brazilian), but the problem is the same.
    >
    > i've searched about this on this group, but i havent found a specific
    > doubt like mine. there were many topics about formatting string (as a
    > variable), but i wanna something to put on a JFormattedTextField (or
    > whatever component is better).
    >
    > thank u all,
    > Cristian Deives
    >
    > E-mail: "cristian deives * gmail!computer".replace(" ",

    ^^^^^^^
    replace(char,char) in java.lang.String cannot be applied to
    (java.lang.String,java.lang.String)
    1 error ;-)
    > "").replace("*", "@").replace("!", ".").replace("puter", "")
    >


    You could remove the currency symbol placeholder ('¤' or '\u00A4') from
    the pattern and then create a number format with this pattern. Something
    like the following:
    NumberFormat curFormat = NumberFormat.getCurrencyInstance();
    if (curFormat instanceof DecimalFormat) {
    String pattern = ((DecimalFormat) curFormat).toPattern();
    System.out.println(pattern);
    String patternWithoutCurSym = pattern.replaceAll("\u00A4","");
    System.out.println(patternWithoutCurSym);
    curFormat = new DecimalFormat(patternWithoutCurSym);
    } else {
    System.err.println("Warning: you need to type the currency symbol");
    }
    JFormattedTextField ftfField = new JFormattedTextField(curFormat);

    Note, that toPattern() is not defined for NumberFormat, but in its
    subclass DecimalFormat. NumberFormat.getCurrencyInstance() always seems
    to return a DecimalFormat instance, though.


    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Jan 22, 2005
    #2
    1. Advertising

  3. CD1

    CD1 Guest

    Thanks, Roland! It worked!

    Now I create a DecimalFormat object with a "#,##0.00" mask =)
    (this mask was taken from what you said, I did a
    getCurrencyInstance(my_locale), System.out.println'ed it, removed the
    currency symbol and now I have this)

    But this way I have to type a dot ".", and not a comma "," to separate
    the numbers. My currency format is like "50,00" and not "50.00". What
    format should I use? I don't understand this formatting.. with #'s and
    0's together :( I tried putting a "," instead of the ".", but it didn't
    work. And now? :(

    PS: my e-mail string is right!! No errors here! Here it is again:

    String my_email = "cristian deives * gmail!computer".replace(" ",
    "").replace("*", "@").replace("!", ".").replace("puter", "");
     
    CD1, Jan 23, 2005
    #3
  4. CD1

    CD1 Guest

    Thanks, Roland! It worked!

    Now I create a DecimalFormat object with a "#,##0.00" mask =)
    (this mask was taken from what you said, I did a
    getCurrencyInstance(my_locale), System.out.println'ed it, removed the
    currency symbol and now I have this)

    But this way I have to type a dot ".", and not a comma "," to separate
    the numbers. My currency format is like "50,00" and not "50.00". What
    format should I use? I don't understand this formatting.. with #'s and
    0's together :( I tried putting a "," instead of the ".", but it didn't
    work. And now? :(

    PS: my e-mail string is right!! No errors here! Here it is again:

    String my_email = "cristian deives * gmail!computer".replace(" ",
    "").replace("*", "@").replace("!", ".").replace("puter", "");
     
    CD1, Jan 23, 2005
    #4
  5. CD1

    Roland Guest

    On 23-1-2005 19:13, CD1 wrote:

    > Thanks, Roland! It worked!
    >
    > Now I create a DecimalFormat object with a "#,##0.00" mask =)
    > (this mask was taken from what you said, I did a
    > getCurrencyInstance(my_locale), System.out.println'ed it, removed the
    > currency symbol and now I have this)
    >
    > But this way I have to type a dot ".", and not a comma "," to separate
    > the numbers. My currency format is like "50,00" and not "50.00". What
    > format should I use? I don't understand this formatting.. with #'s and
    > 0's together :(


    Explained here:
    <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>

    > I tried putting a "," instead of the ".", but it didn't
    > work.


    In the _pattern_, "," always represents the grouping separator and "."
    the decimal separator. The _actual_ separator characters depend, of
    course, on the locale. They are represented by an instance of class
    DecimalFormatSymbols. By creating DecimalFormatSymbols for your locale
    and passing that, together with the pattern, to the DecimalFormat
    constructor, you obtain the appropriate format.

    > And now? :(


    OK, how about this:
    //Locale myLocale = Locale.getDefault();
    //Locale myLocale = new Locale("en", "US");
    Locale myLocale = new Locale("pt", "BR");
    NumberFormat curFormat = NumberFormat.getCurrencyInstance(myLocale);
    if (curFormat instanceof DecimalFormat) {
    String pattern = ((DecimalFormat) curFormat).toPattern();
    String patternWithoutCurSym = pattern.replaceAll("\u00A4\\s", "");
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(myLocale);
    curFormat = new DecimalFormat(patternWithoutCurSym, dfs);
    } else {
    System.err.println("Warning: you need to type the currency symbol");
    }
    JFormattedTextField ftfField = new JFormattedTextField(curFormat);

    Note that the replaceAll() now also removes white space (\s) that follow
    the currency symbol placeholder ('¤' or '\u00A4').
    >
    > PS: my e-mail string is right!! No errors here! Here it is again:
    >
    > String my_email = "cristian deives * gmail!computer".replace(" ",
    > "").replace("*", "@").replace("!", ".").replace("puter", "");


    Yes, believe your email address is right. However I made a little joke
    that it does not compile with Java. If you compile the expression
    "cristian deives * gmail!computer".replace(" ","")
    it will generate an error because the String class has no method replace
    that takes to Strings as parameter, only the method replace with two
    char parameters.
    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Jan 24, 2005
    #5
  6. CD1

    CD1 Guest

    Thanks again Roland!! Now it's the way I want ;)

    But, again with the e-mail thing...

    I know you were making a joke about it, but I was saying "no errors"
    because it compiled fine here! I just checked the Sun documentation
    and, as you said, the String.replace method has (char, char) as
    parameters!! And I didn't know that! :p But it compiles!!! :|

    Is it a bug I just found out?!? My Java is 1.5 Update 01

    Try this simple class...

    public class Email {
    public static void main(String[] args) {
    String my_email = "cristian deives * gmail!computer".replace(" ",
    "").replace("*", "@").replace("!", ".").replace("puter", "");
    System.out.println("My e-mail is " + my_email);
    }
    }

    It works with me.

    Anyway, thanks again for the currency problem!

    PS: I just wanted to make clear that my code, though wrong, is
    compiling (and running)! I knew you were making a joke... ehhehhhe
    Bye!!
     
    CD1, Jan 24, 2005
    #6
  7. CD1

    Roland Guest

    On 24-1-2005 19:47, CD1 wrote:

    > Thanks again Roland!! Now it's the way I want ;)
    >
    > Is it a bug I just found out?!? My Java is 1.5 Update 01

    That explains it.
    > Try this simple class...
    >
    > public class Email {
    > public static void main(String[] args) {
    > String my_email = "cristian deives * gmail!computer".replace(" ",
    > "").replace("*", "@").replace("!", ".").replace("puter", "");
    > System.out.println("My e-mail is " + my_email);
    > }
    > }


    String.replace(CharSequence,CharSequence) is new in 1.5.
    I was compiling with 1.4.2 which resulted in the err.

    Oh well, no bug ;-)

    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Jan 24, 2005
    #7
  8. CD1

    Joined:
    Feb 4, 2011
    Messages:
    1
    Alternative

    If you want to have the jformattedtextfield to display the currency format and edit it as a number, do this:

    1. create the field with the currency formatter.
    jFormattedTextField1 = new javax.swing.JFormattedTextField(NumberFormat.getCurrencyInstance(new Locale("pt", "BR")));

    2. Add a focus gained event listener. When the focus is gained, replace the format of the text.
    private void jFormattedTextField1FocusGained(java.awt.event.FocusEvent evt) {
    DecimalFormat format = new DecimalFormat("#.#", new DecimalFormatSymbols(new Locale("pt", "BR")));
    format.setMaximumFractionDigits(2);
    jFormattedTextField1.setText(format.format((Double)jFormattedTextField1.getValue()));
    jFormattedTextField1.setCaretPosition(0);
    }

    3. Add an input verifier to the field after the initialization.

    jFormattedTextField1.setInputVerifier(new InputVerifier() {

    @Override
    public boolean verify(JComponent input) {
    boolean ret = false;
    try {
    String temp = jFormattedTextField1.getText().replaceAll(",", ".");
    double value = Double.parseDouble(temp);
    ret = true;
    jFormattedTextField1.setValue(value);
    }catch(NumberFormatException ex) {
    ret = false;
    }
    return ret;
    }
    });

    In my case the currency decimal separator is comma. If you use dot it is easier and modify properly.
     
    , Feb 4, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ashish

    currency formatting for Euro

    ashish, Apr 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    473
    ashish
    Apr 27, 2004
  2. Vlad

    Currency Formatting

    Vlad, May 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    4,381
  3. Daniele Varrazzo

    Formatting Currency According to Locale

    Daniele Varrazzo, Apr 28, 2004, in forum: Python
    Replies:
    0
    Views:
    328
    Daniele Varrazzo
    Apr 28, 2004
  4. pebelund
    Replies:
    1
    Views:
    447
    Morten Wennevik
    Oct 23, 2006
  5. RCM
    Replies:
    0
    Views:
    889
Loading...

Share This Page