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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bjorn Brox

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

    Hi!

    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.ToDoubl e() 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
  • Marc Gravell

    #2
    Re: Convert double to and from string alwasy using '.' as separator?

    Use the invariant culture - i.e.

    decimal foo = 12345.6M;
    // change the locale to demo
    Thread.CurrentT hread.CurrentCu lture =
    CultureInfo.Get CultureInfo("fr-FR");
    // this line is locale dependent
    string oops = foo.ToString();
    // these 2 lines are locale independent
    string bar = foo.ToString(Cu ltureInfo.Invar iantCulture);
    decimal foo2 = decimal.Parse(b ar, CultureInfo.Inv ariantCulture);

    Marc


    Comment

    Working...