Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regional settings

 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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+"\"");
}
}
}
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get any exception when I run your code.

The output is:
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do your regional settings specify . as the decimal separator or , ?
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running the code on a Mac, with Danish setting - but the default Locale is "en_US"
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got it to work.

Thank you everyone!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic