Home » C#

C# | Padding an integer number with leading zeros

C# | String.Format() method example: Here, we are going to learn how to pad an integer number with leading zero in C#?
Submitted by IncludeHelp, on November 01, 2019

To pad an integer number with leading zero, we can use String.Format() method which is library method of String class in C#.

using System;

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

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

            Console.WriteLine();  
        }
    }
}

Output

Demo for pad zeros before an integer number:
000256
-000256

In the above program, we are printing 256 and -256 numbers in 6 digits, as we know that 256 has 3 digits and we are padding (left/leading) them with zeros.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.