C# | Padding an integer number with text (minus or zero)

In this tutorial, we will learn how to pad an integer number with text (minus or zero) in C#? By IncludeHelp Last updated : April 09, 2023

Padding an integer number with text (minus or zero)

To pad an integer number with text (minus or zero), we use String.Format() method which is library method of String class in C#. It converts the given value based on the specified format.

Consider the below statement for padding number with text (minus or zero),

String.Format("{0:#;minus #;zero}", 0)

C# code for padding an integer number with text (minus or zero)

using System;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("Demo for zero and negative integer number:");

      Console.WriteLine(String.Format("{0:#;minus #}", 256));
      Console.WriteLine(String.Format("{0:#;minus #}", -256));
      Console.WriteLine(String.Format("{0:#;minus #;zero}", 0));

      Console.WriteLine();
    }
  }
}

Output

Demo for zero and negative integer number:
256
minus 256
zero

Explanation

In the above program, we printed the text "minus 256" in place of -256 and print "zero" in place of 0.




All C#.Net Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.