C# Percent ("P") Format Specifier

CsharpProgrammingServer Side Programming

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 %
raja
Published on 11-Sep-2018 16:56:37
Advertisements