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. static void Main(string[] args)
  5. {
  6. string format = "+#.##;-#.##;(0)";
  7. Console.WriteLine(posNumber.ToString(format));
  8. Console.WriteLine(negNumber.ToString(format));
  9. Console.WriteLine(zeroNumber.ToString(format));
  10. Console.ReadLine();
  11. }
Output

+224.23
-226.26
(0)