Home » C#

C# | Padding an integer number with leading and trailing spaces/zeros

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

To pad an integer number with leading and trailing spaces/zeroes, 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 left or right alignment of an integer number:");

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

            Console.WriteLine();  
        }
    }
}

Output

Demo for left or right alignment of an integer number:
   256
256
 00256
00256

In the above program, 6 is used for right alignment, here space is padded at the left side. And in the case of -6, it is reverse. And we also padded 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.