The "%" custom specifier in C#

CsharpProgrammingServer Side Programming

A % in a format string would multiply a number by 100 before it is formatted.

The percent symbol is added in the number at the location where the % appears.

For an example, declare and initialize a double variable.

double d = .045;

Now, use % custom specifier to multiply number by 100.

d.ToString("#0.##%", CultureInfo.InvariantCulture)

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      double d = .045;
      Console.WriteLine(d.ToString("#0.##%", CultureInfo.InvariantCulture));
      Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:#0.##%}", d));
   }
}

Output

4.5%
4.5%
raja
Published on 12-Sep-2018 16:39:38
Advertisements