Forums Register Login

Regional settings

+Pie Number of slices to send: Send
I'm having some problems with my regional settings, namely with the getCurrencyInstance(). I tried compiling an example class, but got errors. Changing the "$" from the example into local currency format, however, didn't help either.

I don't want to tamper with my regional settings just to compile one class, any ideas how to avoid this and similar problems now and in future code?
+Pie Number of slices to send: Send
Hello Ivan, Welcome to Javaranch...

please post the code that you used...

also change you name to something meaningful...
[ September 05, 2008: Message edited by: Ankit Garg ]
+Pie Number of slices to send: Send
Hi There,

Firstly a quick Admin matter, it's Javaranch policy to specify your full first an last name as you screen name.

Right onto your question, Can you show us the class that you tried to compile and what the error was? Basically we need more information to help you (see my sig on a guide for how to ask questions on Java ranch)
+Pie Number of slices to send: Send
OK, name changed (I didn't see the no ficticios policy, sorry) and here's the code:

import java.text.*;

public class ParseDeclare {

public float parseIt(String s) throws ParseException {

NumberFormat format = NumberFormat.getCurrencyInstance();
Number num = format.parse(s); //may generate exception

return num.floatValue();
}

public static void main(String[] args) {

ParseDeclare parser = new ParseDeclare();
String s;

s = "$45.67";

try {
System.out.println(parser.parseIt(s));
}

catch (ParseException e) {
System.err.println("Invalid string \""+s+"\"");
}

finally {
System.out.println("Original string was \""+s+"\"");
}
}
}
+Pie Number of slices to send: Send
Oh, and the error that appears after I run the class:

init:
deps-jar:
compile-single:
run-single:
Original string was "$45.67"
Invalid string "$45.67"
BUILD SUCCESSFUL (total time: 0 seconds)
____________________________________________________

I am certain it's a problem with the regional settings (mine: Currency 123.456.789,00 kn) and the getCurrencyInstance(), I just need a solution.

As I've said, I tried changing "$45.67" to "24,56 kn" with no effect. That is, except for the 5th and 6th line of the Output changing into:

Original string was "24,56 kn"
Invalid string "24,56 kn"
[ September 05, 2008: Message edited by: Ivan Vrtacnik ]
+Pie Number of slices to send: Send
I don't get any exception when I run your code.

The output is:
+Pie Number of slices to send: Send
Do your regional settings specify . as the decimal separator or , ?
+Pie Number of slices to send: Send
 

Originally posted by Rene Larsen:
I don't get any exception when I run your code.

The output is:



You have the Windows Regional Options set to US, right? That was my point, you won't get this exception in the US, but you will outside of the US. I need this compiled, preferably without adjusting my Settings, so help would be appreciated.

Thanks!
[ September 05, 2008: Message edited by: Ivan Vrtacnik ]
+Pie Number of slices to send: Send
 

Originally posted by Rob Prime:
Do your regional settings specify . as the decimal separator or , ?



The full Currency option is as follows:

Currency: 123.456.789,00 kn
+Pie Number of slices to send: Send
I am running the code on a Mac, with Danish setting - but the default Locale is "en_US"
+Pie Number of slices to send: Send
I have played a little around with your code - the currency symbol "Kn" need to be in the front for it to work...

[ September 05, 2008: Message edited by: Rene Larsen ]
+Pie Number of slices to send: Send
Just as I thought - it is the locale.

When I try the original code but use Locale.US when getting the currency instance, it works like a charm.

I guess that makes sense. $ is unknown in both Croatian and Dutch locales as far as currencies are concerned. Likewise, it would ignore the . and return 4567.0. That's because both in Croatia and in The Netherlands we use , as the decimal symbol, not .
+Pie Number of slices to send: Send
Ok, I got it to work.

Thank you everyone!
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4402 times.
Similar Threads
Post date is not shown
How to detect browser settings?
Client's Regional Settings in JSP
How to detect browser settings?
Strange behavior of the JVM related to Arabization(Localization)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:32:10.