470,596 Members | 1,145 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Convert double to and from string alwasy using '.' as separator?


In germany, norway and France(?) we are using ',' as decimal separator
and it always messes up when you convert a double to and from a string
where the interface expects double values stored as string is using '.'

What parameter shall I use in double.ToString() to ensure that the
outputstring always are using '.' as separator without thousand sep,
and what parameter shall I use to convert double to Double.Parse() or
Convert.ToDouble() to ensure that '.' is expected.

Opposite: How do I parse the string "3,14" to return 3.14 independent of
current locale if trying to parse it as "3.14" fails?

Bjorn Brox
Mar 31 '08 #1
1 3651
Use the invariant culture - i.e.

decimal foo = 12345.6M;
// change the locale to demo
Thread.CurrentThread.CurrentCulture =
// this line is locale dependent
string oops = foo.ToString();
// these 2 lines are locale independent
string bar = foo.ToString(CultureInfo.InvariantCulture);
decimal foo2 = decimal.Parse(bar, CultureInfo.InvariantCulture);

Mar 31 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Little | last post: by
4 posts views Thread by Edwin Knoppert | last post: by
2 posts views Thread by Nicolai.Schoenberg | last post: by
4 posts views Thread by Yoavo | last post: by
5 posts views Thread by Praveen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.