• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regional settings

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get any exception when I run your code.

The output is:
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I got it to work.

Thank you everyone!
 
reply
    Bookmark Topic Watch Topic
  • New Topic