C# Percent ("P") Format Specifier

Description

The percent ("P") specifier formats a number in a percentage.

Item Value
Format specifier "P" or "p"
Supported by All numeric types.
Result Number multiplied by 100 and displayed with a percent symbol.
Precision specifierindicates the desired number of decimal places.
Default precision specifier Defined by System.Globalization.NumberFormatInfo.

Example,

Value Format Formatted
1 ("P", en-US) 100.00 %
1 ("P", fr-FR) 100,00 %
-0.39678 ("P1", en-US) -39.7 %
-0.39678 ("P1", fr-FR)-39,7 %

The NumberFormatInfo properties controls the formatting.

  • PercentPositivePattern
  • PercentNegativePattern
  • NegativeSign
  • PercentSymbol
  • PercentDecimalDigits
  • PercentDecimalSeparator
  • PercentGroupSeparator
  • PercentGroupSizes

Example


using System;/*  w w  w. j  a  va  2s  . c o m*/
using System.Globalization;
class MainClass
{
   static void Main()
   {
        double number = .2468013;
        Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
        // Displays 24.68 %
        Console.WriteLine(number.ToString("P", 
                          CultureInfo.CreateSpecificCulture("hr-HR")));
        // Displays 24,68%     
        Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
        // Displays 24.7 %
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var