Decimal separator and Double.valueOf

M

Mel

Hi,

I have a tricky decimal separator problem in java 1.2
When calling
Double.valueOf("2.5");
everything is fine but on the second machine thios version
Double.valueOf("2,5");
is correct.
Obviously I have to consider localization settings.

My first approach was to use the localized decimal separator
char chDec = new DecimalFormatSymbols().getDecimalSeparator();

String ret = "2.5";
char chDec = new DecimalFormatSymbols().getDecimalSeparator();
if(chDec != ',') ret = ret.replace(',', chDec);
if(chDec != '.') ret = ret.replace('.', chDec);
Double.valueOf(ret);

To my surprise this didn't work too because my machine can read
Double.valueOf("2.5");
but the localised version looks like this
Double.valueOf("2,5");

To get rid of the localisation settings I tried this version
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
df.setDecimalFormatSymbols(dfs);
return df.parse(StrEnforceDecimalDot(strVal)).doubleValue();
This works fine on one machine but not on the other. Is there something
wrong with the code.


Machine 1 looks like this:
WinXp German version -> Oracle9iRel2 -> Java running within Oracle

Machine 1 looks like this:
Unix -> Oracle9iRel2 -> Java running within Oracle

What am I missing?
Which strings can Double.valueOf parse per definition.
I can't find a clear specification in the java doc.
Do I have to consider Oracle NLS parameters?

Thanks for any help.
Mel
 
M

Michael Borgwardt

Mel said:
What am I missing?
Which strings can Double.valueOf parse per definition.
I can't find a clear specification in the java doc.

Use java.text.DecimalFormat instead. THat's what it's for
(and Double.valueOf()) isn't!
 
M

Mel

Thanks for your response.
Double.valueOf
returns a new double initialized to the value of the passed string.

I pass the string "2.5"
And I expect to get the value 2.5
why doesn't this work on each machine?

java.text.DecimalFormat is intended for formatting numbers.
I don't need number formatting, I just need to parse text strings like
"2.5"
"34.7"
"78.56"

without being affected from localisation.

Mel said:
What am I missing?
Which strings can Double.valueOf parse per definition.
I can't find a clear specification in the java doc.

Use java.text.DecimalFormat instead. THat's what it's for
(and Double.valueOf()) isn't!
 
M

Michael Borgwardt

Mel said:
Double.valueOf
returns a new double initialized to the value of the passed string.

I pass the string "2.5"
And I expect to get the value 2.5
why doesn't this work on each machine?

Actually, it should. According to the API doc, Double.valueOf expects
the input to be floating point literals according to the Java Language
Specification. Which is not locale-dependant.

If it doesn't work on your machine, then either you are not calling
the method you think you're calling, not passing it the String you
think you're passing it, or your Java implementation is broken.
java.text.DecimalFormat is intended for formatting numbers.

*and* parsing them. It allows you full control over the parsing
process.
 
M

Mel

Thanks
Double.valueOf ("2.5");
is independent of any localisation and works everywhere
This statement will actually help me
Thanks

Mel said:
Double.valueOf
returns a new double initialized to the value of the passed string.

I pass the string "2.5"
And I expect to get the value 2.5
why doesn't this work on each machine?

Actually, it should. According to the API doc, Double.valueOf expects
the input to be floating point literals according to the Java Language
Specification. Which is not locale-dependant.

If it doesn't work on your machine, then either you are not calling
the method you think you're calling, not passing it the String you
think you're passing it, or your Java implementation is broken.
java.text.DecimalFormat is intended for formatting numbers.

*and* parsing them. It allows you full control over the parsing
process.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top