The "%" custom specifier in C#


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%

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements