Non-locale-specific floating point numbers

M

Mike Schilling

I want to write some C# that converts between floating-point numbers and
strings in a non-locale-specific way. That is, I want decimal points always
to be ".", etc. The simple overloads of Double.ToString() and
Double.Parse() change their behavior depending on locale. I'm considering
creating a static en-us locale and passing it to the overloads of Parse and
ToString that take a locale, but is there a better way?
 
R

Rad [Visual C# MVP]

I want to write some C# that converts between floating-point numbers and
strings in a non-locale-specific way. That is, I want decimal points always
to be ".", etc. The simple overloads of Double.ToString() and
Double.Parse() change their behavior depending on locale. I'm considering
creating a static en-us locale and passing it to the overloads of Parse and
ToString that take a locale, but is there a better way?

Unless i'm mistaken a float is a float inependent of locale. The comma
separators, decimal points etc only become an issue when you're
*displaying* the float.

When it comes to parsing, what I would do is change the locale of the
running application to be something I can work with e.g. en-US or en-GB by
setting the System.Threading.CurrentThread.Culture to the locale i want to
use
 
M

Mike Schilling

Rad said:
Unless i'm mistaken a float is a float inependent of locale. The comma
separators, decimal points etc only become an issue when you're
*displaying* the float.

That is, coverting it to a string, which is what I'm asking about.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Mike said:
I want to write some C# that converts between floating-point numbers and
strings in a non-locale-specific way. That is, I want decimal points always
to be ".", etc. The simple overloads of Double.ToString() and
Double.Parse() change their behavior depending on locale. I'm considering
creating a static en-us locale and passing it to the overloads of Parse and
ToString that take a locale, but is there a better way?

I think it is much nicer to pass a locale to the methods
as you suggest than messing with the global one.

Arne
 
J

Jon Skeet [C# MVP]

Mike Schilling said:
I want to write some C# that converts between floating-point numbers and
strings in a non-locale-specific way. That is, I want decimal points always
to be ".", etc. The simple overloads of Double.ToString() and
Double.Parse() change their behavior depending on locale. I'm considering
creating a static en-us locale and passing it to the overloads of Parse and
ToString that take a locale, but is there a better way?

Instead of creating a static en-us, use CultureInfo.InvariantCulture.
Pass that to methods as appropriate.

Of course, you could always wrap that up in your own helper class if
you're doing it a lot.
 
M

Mike Schilling

Jon Skeet said:
Instead of creating a static en-us, use CultureInfo.InvariantCulture.
Pass that to methods as appropriate.

Of course, you could always wrap that up in your own helper class if
you're doing it a lot.

Which is what I've done, thnks.

I can't be the only person with this requirement. Any program that defines
an invariant floating-point format (csc, for example) needs it.
 

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

Top