Creating Blank if zero decimal format string
-
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
-
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
-
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###;#")
-
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
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
-
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
-
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
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,12Using empty string
(0m).ToString() => 0
(1.10m).ToString() => 1,10
(1.1m).ToString() => 1,1
(1.123m).ToString() => 1,123Using G
(0m).ToString("G") => 0
(1.10m).ToString("G") => 1,10
(1.1m).ToString("G") => 1,1
(1.123m).ToString("G") => 1,123So are yuo actually looking for "G" like behaviour? AFAIK you must implement your own converter which has the knowledge of the actual input string.
-
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,12Using empty string
(0m).ToString() => 0
(1.10m).ToString() => 1,10
(1.1m).ToString() => 1,1
(1.123m).ToString() => 1,123Using G
(0m).ToString("G") => 0
(1.10m).ToString("G") => 1,10
(1.1m).ToString("G") => 1,1
(1.123m).ToString("G") => 1,123So are yuo actually looking for "G" like behaviour? AFAIK you must implement your own converter which has the knowledge of the actual input string.
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
-
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
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.