Format Positive And Negative Number In C#

Sometimes we need to format Numbers with Positive (+) and Negative (-) signs which we can achieve easily using string formatting.

Given below is string format which has three parts separated by a semicolon, the first is for positive numbers, the second is for negative numbers and the third is for zero. 

  1.        private static double posNumber = 224.233;  
  2.        private static double negNumber = -226.256;  
  3.        private static double zeroNumber = 0;  
  4.   
  5.        static void Main(string[] args)  
  6.        {  
  7.            string format = "+#.##;-#.##;(0)";  
  8.   
  9.            Console.WriteLine(posNumber.ToString(format));  
  10.            Console.WriteLine(negNumber.ToString(format));  
  11.            Console.WriteLine(zeroNumber.ToString(format));  
  12.   
  13.            Console.ReadLine();  
  14.        }  
Output

+224.23
-226.26
(0)