C# Percent ("P") Format Specifier


The percent ("P") format specifier is used to multiply a number by 100.

It converts the number into a string representing a % (percentage).

We have the following double type −

double val = .975746;

If we will set (“P”) format specifier, then the above would result as −

97.57 %

In the same way, using (“P1”), would only include a single vale after decimal-point.

97.6%

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      double val = .975746;
      Console.WriteLine(val.ToString("P", CultureInfo.InvariantCulture));
      Console.WriteLine(val.ToString("P1", CultureInfo.InvariantCulture));
      Console.WriteLine(val.ToString("P4", CultureInfo.InvariantCulture));
   }
}

Output

97.57 %
97.6 %
97.5746 %

Updated on: 23-Jun-2020

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements