File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Regional settings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regional settings" Watch "Regional settings" New topic
Author

Regional settings

Ivan Vrtacnik
Greenhorn

Joined: Sep 02, 2008
Posts: 26
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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 ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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)


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Ivan Vrtacnik
Greenhorn

Joined: Sep 02, 2008
Posts: 26
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

Joined: Sep 02, 2008
Posts: 26
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

Joined: Oct 12, 2001
Posts: 1179

I don't get any exception when I run your code.

The output is:


Regards, Rene Larsen
Dropbox Invite
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Do your regional settings specify . as the decimal separator or , ?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ivan Vrtacnik
Greenhorn

Joined: Sep 02, 2008
Posts: 26
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

Joined: Sep 02, 2008
Posts: 26
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

Joined: Oct 12, 2001
Posts: 1179

I am running the code on a Mac, with Danish setting - but the default Locale is "en_US"
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Sep 02, 2008
Posts: 26
Ok, I got it to work.

Thank you everyone!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regional settings
 
Similar Threads
Client's Regional Settings in JSP
Strange behavior of the JVM related to Arabization(Localization)
How to detect browser settings?
How to detect browser settings?
Post date is not shown