C# Decimal ("D") Format Specifier


The "D" (or decimal) format specifier works for integer type. It converts a number to a string of decimal digits (0-9).

Let’say the following is our number.

int val = 467;

Now to return the result as 0467, use the following decimal format specifier.

val.ToString("D4")

Let us see another example.

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      int val;
      val = 877;
      Console.WriteLine(val.ToString("D"));
      Console.WriteLine(val.ToString("D4"));
      Console.WriteLine(val.ToString("D8"));
   }
}

Output

877
0877
00000877

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements