Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > currency formatting in a MaskFormater

Reply
Thread Tools

currency formatting in a MaskFormater

 
 
CD1
Guest
Posts: n/a
 
      01-22-2005
hi guys!

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

JFormattedTextField ftfField = new
JFormattedTextField(NumberFormat.getCurrencyInstan ce(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", "")

 
Reply With Quote
 
 
 
 
Roland
Guest
Posts: n/a
 
      01-22-2005
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.getCurrencyInstan ce(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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
 
 
 
CD1
Guest
Posts: n/a
 
      01-23-2005
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", "");

 
Reply With Quote
 
CD1
Guest
Posts: n/a
 
      01-23-2005
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", "");

 
Reply With Quote
 
Roland
Guest
Posts: n/a
 
      01-24-2005
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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
CD1
Guest
Posts: n/a
 
      01-24-2005
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! 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!!

 
Reply With Quote
 
Roland
Guest
Posts: n/a
 
      01-24-2005
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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
fabio.yamada@gmail.com fabio.yamada@gmail.com is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-04-2011
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.getCu rrencyInstance(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.Foc usEvent 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview Currency Formatting - BoundField vs. Template Field RCM ASP .Net 0 01-17-2007 04:51 PM
Converting string to currency/formatting in thousands.aspx (.NET) pebelund ASP .Net 1 10-23-2006 10:32 AM
Currency Formatting Vlad ASP .Net 3 05-26-2004 01:52 PM
Formatting Currency According to Locale Daniele Varrazzo Python 0 04-28-2004 04:46 PM
currency formatting for Euro ashish ASP .Net 0 04-27-2004 08:36 PM



Advertisments