Format decimal thousands

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    Format decimal thousands

    Hi all,
    with this code:

    decimal? test = 1200345.56m;
    decimal? test2 = Decimal.Parse(S tring.Format("{ 0:0,0.000}", test));

    i can not obtain a decimal value with thousands separators (but without
    Decimal.Parse do).

    How can i solve?

    Thanks in advance
    --
    Luigi

  • Marc Gravell

    #2
    Re: Format decimal thousands

    Can you clarify what you are trying to do? A decimal is just a number -
    it never really contains any separators. The string version might, but
    you've already demonstrated that with your String.Format and Parse usage.

    Marc

    Comment

    • GArlington

      #3
      Re: Format decimal thousands

      On Sep 15, 2:15 pm, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
      Hi all,
      with this code:
      >
      decimal? test = 1200345.56m;
      decimal? test2 = Decimal.Parse(S tring.Format("{ 0:0,0.000}", test));
      As you have multiple possible decimal separators [BOTH comma AND DOT
      are used in different locales] in your string, you will have to use
      the Parse function specifying which particular format applies...
      >
      i  can not obtain a decimal value with thousands separators (but without
      Decimal.Parse do).
      >
      How can i solve?
      >
      Thanks in advance
      --
      Luigi

      Comment

      • Marc Gravell

        #4
        Re: Format decimal thousands

        I don't see why... both the Format and Parse will take the culture into
        account. In the format string the . and , are palceholders for the
        decimal and thousand separators (they aren't used verbatim). So this
        code should work "as is":

        static decimal? TestRoundtrip()
        {
        decimal? test = 1200345.56m;
        string s = String.Format(" {0:0,0.000}", test);
        decimal? test2 = Decimal.Parse(s );
        Console.WriteLi ne(test2);
        return test2;
        }
        static void Main()
        {
        decimal? a, b;
        Thread.CurrentT hread.CurrentCu lture =
        CultureInfo.Get CultureInfo("fr-FR");
        a=TestRoundtrip ();
        Thread.CurrentT hread.CurrentCu lture =
        CultureInfo.Get CultureInfo("en-GB");
        b=TestRoundtrip ();
        Console.WriteLi ne(a==b); // returns true
        }

        Comment

        Working...