Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating Blank if zero decimal format string

Creating Blank if zero decimal format string

Scheduled Pinned Locked Moved C#
csharptutorialquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndrusM
    wrote on last edited by
    #1

    How to create format string for decimal data type which shows blank if number is zero and otherwize in default format? I tried format string f;;# but this shows f for nonzero numbers. Is it possible to create such string for .NET decimal ToString() ? Andrus. using System.Windows.Forms; class AppMainEntry { static void Main() { // Expected: 0.40 actual: f MessageBox.Show((0.40m).ToString("f;;#")); // expected: 123.40 actual: f MessageBox.Show((123.40m).ToString("f;;#")); // expected: 123.4 actual: f MessageBox.Show((123.4m).ToString("f;;#")); // OK: expected: empty actual: empty MessageBox.Show((0m).ToString("f;;#")); } }

    Andrus

    W 1 Reply Last reply
    0
    • A AndrusM

      How to create format string for decimal data type which shows blank if number is zero and otherwize in default format? I tried format string f;;# but this shows f for nonzero numbers. Is it possible to create such string for .NET decimal ToString() ? Andrus. using System.Windows.Forms; class AppMainEntry { static void Main() { // Expected: 0.40 actual: f MessageBox.Show((0.40m).ToString("f;;#")); // expected: 123.40 actual: f MessageBox.Show((123.40m).ToString("f;;#")); // expected: 123.4 actual: f MessageBox.Show((123.4m).ToString("f;;#")); // OK: expected: empty actual: empty MessageBox.Show((0m).ToString("f;;#")); } }

      Andrus

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      AFAIK you cannot mix standard format elements (f) in custom format so you'll have to create the whole format string. For example

      (0.40m).ToString("###,##0.00###;-###,##0.00###;#")

      A 1 Reply Last reply
      0
      • W Wendelius

        AFAIK you cannot mix standard format elements (f) in custom format so you'll have to create the whole format string. For example

        (0.40m).ToString("###,##0.00###;-###,##0.00###;#")

        A Offline
        A Offline
        AndrusM
        wrote on last edited by
        #3

        For 1.1m your format shows 2 places after comma, but I want only 1 place as F. Which custome format is exactly equivalent to F format ? Andrus.

        Andrus

        W 1 Reply Last reply
        0
        • A AndrusM

          For 1.1m your format shows 2 places after comma, but I want only 1 place as F. Which custome format is exactly equivalent to F format ? Andrus.

          Andrus

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          If you want just one zero after comma, change the 0 to #:

          (1.1m).ToString("###,##0.00###;-###,##0.00###;#")

          to

          (1.1m).ToString("###,##0.0####;-###,##0.0####;#")

          I'm not sure about F's format since I believe that it is affected by number settings in control panel. For more info Standard Numeric Format Strings[^] and Custom Numeric Format Strings[^] Mika

          A 1 Reply Last reply
          0
          • W Wendelius

            If you want just one zero after comma, change the 0 to #:

            (1.1m).ToString("###,##0.00###;-###,##0.00###;#")

            to

            (1.1m).ToString("###,##0.0####;-###,##0.0####;#")

            I'm not sure about F's format since I believe that it is affected by number settings in control panel. For more info Standard Numeric Format Strings[^] and Custom Numeric Format Strings[^] Mika

            A Offline
            A Offline
            AndrusM
            wrote on last edited by
            #5

            F format behaviour on decimal is not affected by any settings: 0m returns 0 1.10m returns 1.10 1.1m returns 1.1 1.123m returns 1.123 How to obtain same behaviour with format string except zero is converted to blank string ?

            Andrus

            W 1 Reply Last reply
            0
            • A AndrusM

              F format behaviour on decimal is not affected by any settings: 0m returns 0 1.10m returns 1.10 1.1m returns 1.1 1.123m returns 1.123 How to obtain same behaviour with format string except zero is converted to blank string ?

              Andrus

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              From the manual: If format is null or an empty string, the return value of this instance is formatted with the general numeric format specifier ("G"). Simple test case:

              System.Diagnostics.Debug.WriteLine("-------");
              System.Diagnostics.Debug.WriteLine("Using F");
              System.Diagnostics.Debug.WriteLine("-------");
              System.Diagnostics.Debug.WriteLine("(0m).ToString(\"F\") => " + (0m).ToString("F"));
              System.Diagnostics.Debug.WriteLine("(1.10m).ToString(\"F\") => " + (1.10m).ToString("F"));
              System.Diagnostics.Debug.WriteLine("(1.1m).ToString(\"F\") => " + (1.1m).ToString("F"));
              System.Diagnostics.Debug.WriteLine("(1.123m).ToString(\"F\") => " + (1.123m).ToString("F"));

              System.Diagnostics.Debug.WriteLine("------------------");
              System.Diagnostics.Debug.WriteLine("Using empty string");
              System.Diagnostics.Debug.WriteLine("------------------");
              System.Diagnostics.Debug.WriteLine("(0m).ToString() => " + (0m).ToString(""));
              System.Diagnostics.Debug.WriteLine("(1.10m).ToString() => " + (1.10m).ToString(""));
              System.Diagnostics.Debug.WriteLine("(1.1m).ToString() => " + (1.1m).ToString(""));
              System.Diagnostics.Debug.WriteLine("(1.123m).ToString() => " + (1.123m).ToString(""));

              System.Diagnostics.Debug.WriteLine("-------");
              System.Diagnostics.Debug.WriteLine("Using G");
              System.Diagnostics.Debug.WriteLine("-------");
              System.Diagnostics.Debug.WriteLine("(0m).ToString(\"G\") => " + (0m).ToString("G"));
              System.Diagnostics.Debug.WriteLine("(1.10m).ToString(\"G\") => " + (1.10m).ToString("G"));
              System.Diagnostics.Debug.WriteLine("(1.1m).ToString(\"G\") => " + (1.1m).ToString("G"));
              System.Diagnostics.Debug.WriteLine("(1.123m).ToString(\"G\") => " + (1.123m).ToString("G"));

              results:

              -------
              Using F

              (0m).ToString("F") => 0,00
              (1.10m).ToString("F") => 1,10
              (1.1m).ToString("F") => 1,10
              (1.123m).ToString("F") => 1,12

              Using empty string

              (0m).ToString() => 0
              (1.10m).ToString() => 1,10
              (1.1m).ToString() => 1,1
              (1.123m).ToString() => 1,123

              Using G

              (0m).ToString("G") => 0
              (1.10m).ToString("G") => 1,10
              (1.1m).ToString("G") => 1,1
              (1.123m).ToString("G") => 1,123

              So are yuo actually looking for "G" like behaviour? AFAIK you must implement your own converter which has the knowledge of the actual input string.

              A 1 Reply Last reply
              0
              • W Wendelius

                From the manual: If format is null or an empty string, the return value of this instance is formatted with the general numeric format specifier ("G"). Simple test case:

                System.Diagnostics.Debug.WriteLine("-------");
                System.Diagnostics.Debug.WriteLine("Using F");
                System.Diagnostics.Debug.WriteLine("-------");
                System.Diagnostics.Debug.WriteLine("(0m).ToString(\"F\") => " + (0m).ToString("F"));
                System.Diagnostics.Debug.WriteLine("(1.10m).ToString(\"F\") => " + (1.10m).ToString("F"));
                System.Diagnostics.Debug.WriteLine("(1.1m).ToString(\"F\") => " + (1.1m).ToString("F"));
                System.Diagnostics.Debug.WriteLine("(1.123m).ToString(\"F\") => " + (1.123m).ToString("F"));

                System.Diagnostics.Debug.WriteLine("------------------");
                System.Diagnostics.Debug.WriteLine("Using empty string");
                System.Diagnostics.Debug.WriteLine("------------------");
                System.Diagnostics.Debug.WriteLine("(0m).ToString() => " + (0m).ToString(""));
                System.Diagnostics.Debug.WriteLine("(1.10m).ToString() => " + (1.10m).ToString(""));
                System.Diagnostics.Debug.WriteLine("(1.1m).ToString() => " + (1.1m).ToString(""));
                System.Diagnostics.Debug.WriteLine("(1.123m).ToString() => " + (1.123m).ToString(""));

                System.Diagnostics.Debug.WriteLine("-------");
                System.Diagnostics.Debug.WriteLine("Using G");
                System.Diagnostics.Debug.WriteLine("-------");
                System.Diagnostics.Debug.WriteLine("(0m).ToString(\"G\") => " + (0m).ToString("G"));
                System.Diagnostics.Debug.WriteLine("(1.10m).ToString(\"G\") => " + (1.10m).ToString("G"));
                System.Diagnostics.Debug.WriteLine("(1.1m).ToString(\"G\") => " + (1.1m).ToString("G"));
                System.Diagnostics.Debug.WriteLine("(1.123m).ToString(\"G\") => " + (1.123m).ToString("G"));

                results:

                -------
                Using F

                (0m).ToString("F") => 0,00
                (1.10m).ToString("F") => 1,10
                (1.1m).ToString("F") => 1,10
                (1.123m).ToString("F") => 1,12

                Using empty string

                (0m).ToString() => 0
                (1.10m).ToString() => 1,10
                (1.1m).ToString() => 1,1
                (1.123m).ToString() => 1,123

                Using G

                (0m).ToString("G") => 0
                (1.10m).ToString("G") => 1,10
                (1.1m).ToString("G") => 1,1
                (1.123m).ToString("G") => 1,123

                So are yuo actually looking for "G" like behaviour? AFAIK you must implement your own converter which has the knowledge of the actual input string.

                A Offline
                A Offline
                AndrusM
                wrote on last edited by
                #7

                Yes. 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, with empty format string or with G format as you demostrated produces this output except it outputs 0m as 0 : 0.400m is converted to 0.400 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. I'm using FYI RDL report engine which allows to use only .NET format strings, no code.

                Andrus

                W 1 Reply Last reply
                0
                • A AndrusM

                  Yes. 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, with empty format string or with G format as you demostrated produces this output except it outputs 0m as 0 : 0.400m is converted to 0.400 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. I'm using FYI RDL report engine which allows to use only .NET format strings, no code.

                  Andrus

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  Sorry to say, but don't know any solution by using only format strings. If you are using this Project RDL - Open Source Report Definition Language implementation in C#[^] I noticed that expressions and user written functions are supported (for example iif). Perhaps using these would help you. Another option could be to preformat the numbers before they are given to report engine (converting them to strings) in which case you can control the output in your code.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups