Creating Blank if zero decimal format string

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

    Creating Blank if zero decimal format string

    How to create format string for decimal data type
    which shows blank for zero and default format otherwize ?

    I tried format string "f;f;#" but this shows f for nonzero numbers.

    Andrus.


    using System.Windows. Forms;

    class AppMainEntry
    {
    static void Main()
    {
    // Expected: 0.40 actual: f
    MessageBox.Show ((0.40m).ToStri ng("f;;#"));

    // expected: 123.40 actual: f
    MessageBox.Show ((123.40m).ToSt ring("f;;#"));

    // expected: 123.4 actual: f
    MessageBox.Show ((123.4m).ToStr ing("f;;#"));

    // OK: expected: empty actual: empty
    MessageBox.Show ((0m).ToString( "f;;#"));
    }
    }
  • Peter Duniho

    #2
    Re: Creating Blank if zero decimal format string

    On Sat, 30 Aug 2008 06:39:46 -0700, Andrus <kobruleht2@hot .eewrote:
    How to create format string for decimal data type
    which shows blank for zero and default format otherwize ?
    I tried format string "f;f;#" but this shows f for nonzero numbers.
    That's because 'f' isn't a valid character in custom numeric format
    strings.

    Learn how to create a custom numeric format string to format numeric data in .NET. A custom numeric format string has one or more custom numeric specifiers.


    You can use the standard numeric format strings or you can write a custom
    numeric format string. You can't mix and match.

    Pete

    Comment

    • Andrus

      #3
      Re: Creating Blank if zero decimal format string

      That's because 'f' isn't a valid character in custom numeric formatYes, I have read this page several times.
      You can use the standard numeric format strings or you can write a custom
      numeric format string. You can't mix and match.
      Sure.
      Custom format strings discard leading zero if # format char is used or show
      zeroes always if 9 format char is used.
      Only standard format string which produces desired output seems top be f
      format.
      However f format shows zero instead of blank.

      How to create format string which is like standard f format string but shows
      blank if value is zero ?

      Andrus.

      Comment

      • Peter Duniho

        #4
        Re: Creating Blank if zero decimal format string

        On Sat, 30 Aug 2008 10:04:57 -0700, Andrus <kobruleht2@hot .eewrote:
        [...]
        Custom format strings discard leading zero if # format char is used or
        show zeroes always if 9 format char is used.
        '9' isn't a valid custom numeric format string character. So, I don't
        know what you mean here.
        Only standard format string which produces desired output seems top be f
        format.
        However f format shows zero instead of blank.
        The custom numeric format "0.00" is one custom format string that is
        essentially equivalent to "F".
        How to create format string which is like standard f format string but
        shows blank if value is zero ?
        Use a valid custom numeric format string that correctly describes your
        desired output, instead of the character 'f' or 'F'. It being a custom
        numeric format, you can use whatever you like, as long as it's valid. But
        you might want to start with "0.00".

        Pete

        Comment

        • Andrus

          #5
          Re: Creating Blank if zero decimal format string

          '9' isn't a valid custom numeric format string character. So, I don't
          know what you mean here.
          I'm sorry I meant '0'
          The custom numeric format "0.00" is one custom format string that is
          essentially equivalent to "F".
          0.00 shows always 2 places after comma
          F format shows as many places after comma as decimal contains.
          So I don't understand how this is same as F
          >How to create format string which is like standard f format string but
          >shows blank if value is zero ?
          >
          Use a valid custom numeric format string that correctly describes your
          desired output, instead of the character 'f' or 'F'. It being a custom
          numeric format, you can use whatever you like, as long as it's valid. But
          you might want to start with "0.00".
          0.00 shows always 2 places after comma.
          I want custom format which is same as F

          I tried

          "#" - it displays .45 i.e without leading zero

          "0.#;0.#;#" shows 1.1 instead of 1.10 for decimal 1.10m

          Which custom format is same as F ?

          Andrus.

          Comment

          • Peter Duniho

            #6
            Re: Creating Blank if zero decimal format string

            On Sun, 31 Aug 2008 08:54:33 -0700, Andrus <kobruleht2@hot .eewrote:
            [...]
            >The custom numeric format "0.00" is one custom format string that is
            >essentially equivalent to "F".
            >
            0.00 shows always 2 places after comma
            F format shows as many places after comma as decimal contains.
            No, it doesn't. If you don't specify a precision, it uses the current
            culture's NumberFormatInf o.NumberDecimal Digits property. All you have to
            do to match the behavior as "F" is to use the same number of digits in
            your custom format as the NumberDecimalDi gits property indicates.

            On my installation (culture of US English), the default precision is 2,
            and that's how many decimal places "F" shows, even if there are no
            significant digits to the right of the decimal.
            [...]
            >Use a valid custom numeric format string that correctly describes your
            >desired output, instead of the character 'f' or 'F'. It being a custom
            >numeric format, you can use whatever you like, as long as it's valid.
            >But you might want to start with "0.00".
            >
            0.00 shows always 2 places after comma.
            For the US English culture, so does "F". Your own culture settings might
            be different. But "F" does not take into account the number itself when
            deciding how many places to show.
            I want custom format which is same as F
            Then just write a format like "0.00", except with as many decimal digits
            as is appropraite for the culture you're using. If you want to do this
            dynamically, you could use something like this:

            int cdigitDecimal =
            CultureInfo.Cur rentCulture.Num berFormat.Numbe rDecimalDigits;
            string strDecimalForma t = "{1:0." +
            new String('0', cdigitDecimal) + "}";

            Then just use that string wherever you need it (for example, in building
            the custom format string that handles zero the way you want).

            Pete

            Comment

            • Andrus

              #7
              Re: Creating Blank if zero decimal format string

              Then just use that string wherever you need it (for example, in building
              the custom format string that handles zero the way you want).
              Thank you. I'm sorry my previous letter was wrong, f format cannot used.

              I want that result string contains as may decimal places after comma as
              decimal value actually contains
              *except* zero must be converted to empty string.

              ToString() without argument or with empty format string produces this output
              except it outputs 0m as 0 :

              0.400m is converted to 0.4000
              123.40m is converted to 123.40
              123.4m is converted to 123.4
              0m is converted to 0

              How to create format string which works as above except 0m, 0.00m etc is
              converted to empty string ?
              I tried ";;#" format string but this does not output any digits.

              Andrus.

              Comment

              • Peter Duniho

                #8
                Re: Creating Blank if zero decimal format string

                On Sun, 31 Aug 2008 13:28:11 -0700, Andrus <kobruleht2@hot .eewrote:
                [...]
                ToString() without argument or with empty format string produces this
                output except it outputs 0m as 0 :
                >
                0.400m is converted to 0.4000
                123.40m is converted to 123.40
                123.4m is converted to 123.4
                0m is converted to 0
                So, you want the behavior of the "G" code, not the "F" code.

                As far as I know, there are no custom formatting codes that do that. I
                would not disagree if you felt this was an oversight in .NET.
                How to create format string which works as above except 0m, 0.00m etc is
                converted to empty string ?
                I tried ";;#" format string but this does not output any digits.
                What about just writing a method that returns the formatted string you
                want? Does this have to be a formatting code?

                Pete

                Comment

                Working...