double string convert

  • Thread starter Thread starter Nicolai.Schoenberg
  • Start date Start date
N

Nicolai.Schoenberg

When i try it like you say, he ignores the dot.

For example

s = "12.45";
d = double.Parse(s);

d will be 1245.0 :(

pls help
 
When i try it like you say, he ignores the dot.

For example

s = "12.45";
d = double.Parse(s);

d will be 1245.0 :(

What are your culture settings? Clearly, parsing "12.45" as a double
would normally work in the context of a US-configured computer. I will
hazard a guess that you're using the comma for the decimal separator, and
the period for the thousands separator. If that's the case, then you need
to parse "12,45" if you expect to have a value returned that is twelve and
forty-five hundredths.

There are overloads for the Parse() method that take culture-specific
format providers, so if you know the string is from a specific culture
that may be different from what the user has configured, you can use those
to ensure correct conversion.

Pete
 
When i try it like you say, he ignores the dot.

For example

s = "12.45";
d = double.Parse(s);

d will be 1245.0 :(

This is because the default Culture that you are using defines de dot as
a thousands separator instead of a decimal point. You can either write your
string with a comma for the decimal point ("12,45"), or change the
CurrentCulture, or change the FormatProvider of the Parse method:

using System.Globalization;
....
CultureInfo ci = new CultureInfo("en-US");
....
s = "12.45";
d = double.Parse(s, ci);
 

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

Back
Top