Custom Formatting for Integers in C#

Formatting Integers: This C# example demonstrate the custom formatting for integers. By IncludeHelp Last updated : April 09, 2023

To print the integer values with customized or specified format, we use String.Format() function. It accepts a value and expression in which we have to format the given integer value.

C# program for formatting Integers

In this program, we will format three values in the different three formats.

Here, we are using two placeholders as custom string to format integer,

  • Zero placeholder (0)
  • Digit placeholder (#)
using System;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("Demo for custom formatting of an integer number:");

      Console.WriteLine(String.Format("{0:+#### #### ####}", 1234567891012));
      Console.WriteLine(String.Format("{0:##-##-####}", 21102019));
      Console.WriteLine(String.Format("{0:##/##/####}", 21102019));

      Console.WriteLine();
    }
  }
}

Output

Demo for custom formatting of an integer number:
+12345 6789 1012
21-10-2019
21/10/2019

In the above program, we printed sign '+' and printed the numbers in 4 digits three times. And here we printed the date also with symbol '-' and '/'.




All C#.Net Programs

Comments and Discussions!

Load comments ↻





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