String format number with sign
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Program { static void Main(string[] args) { //this line create an int variable. int number = 12345; Console.WriteLine("formatted string.............."); int length = number.ToString().Length + 1; int length2 = length + 1; Console.WriteLine("Number: " + number); //format string using ToString method Console.WriteLine("number padding with plus(+) sign: " + number.ToString("+#")); //format string using string.format method. Console.WriteLine("number padding with plus sign: " + string.Format("+{0}", number)); //another technique to format string using padleft method. Console.WriteLine("\nnumber padding with '#' sign double pad: " + number.ToString().PadLeft(length2, '#')); Console.WriteLine("number padding with '@' sign: " + number.ToString().PadLeft(length, '@')); Console.WriteLine("number padding with dollar($) sign: " + number.ToString().PadLeft(length, '$')); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | formatted string.............. Number: 12345 number padding with plus(+) sign: +12345 number padding with plus sign: +12345 number padding with '#' sign double pad: ##12345 number padding with '@' sign: @12345 number padding with dollar($) sign: $12345 |