logo
CSharp_Prog_Guide

Бесконечности действительных чисел с плавающей запятой и NaN

Обратите внимание, что независимо от типа форматирования, если значением переменной с плавающей запятой Single или Double является плюс бесконечность, минус бесконечность или не является числом (NaN), ее форматированное выражение является значением соответствующего свойства PositiveInfinity, NegativeInfinity, или NaN, которые указаны текущим объектом NumberFormatInfo.

Example

The following example formats an integral and a floating-point numeric value using the en-us culture and all the standard numeric format specifiers. This example uses two particular numeric types, but would yield similar results for any of the numeric base types (Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Decimal, Single, and Double).

using System;

using System.Globalization;

using System.Threading;

public class NumericFormats

{

public static void Main()

{

// Display string representations of numbers for en-us culture

CultureInfo ci = new CultureInfo("en-us");

// Output floating point values

double floating = 10761.937554;

Console.WriteLine("C: {0}",

floating.ToString("C", ci)); // Displays "C: $10,761.94"

Console.WriteLine("E: {0}",

floating.ToString("E03", ci)); // Displays "E: 1.076E+004"

Console.WriteLine("F: {0}",

floating.ToString("F04", ci)); // Displays "F: 10761.9376"

Console.WriteLine("G: {0}",

floating.ToString("G", ci)); // Displays "G: 10761.937554"

Console.WriteLine("N: {0}",

floating.ToString("N03", ci)); // Displays "N: 10,761.938"

Console.WriteLine("P: {0}",

(floating/10000).ToString("P02", ci)); // Displays "P: 107.62 %"

Console.WriteLine("R: {0}",

floating.ToString("R", ci)); // Displays "R: 10761.937554"

Console.WriteLine();

// Output integral values

int integral = 8395;

Console.WriteLine("C: {0}",

integral.ToString("C", ci)); // Displays "C: $8,395.00"

Console.WriteLine("D: {0}",

integral.ToString("D6", ci)); // Displays D: 008395""

Console.WriteLine("E: {0}",

integral.ToString("E03", ci)); // Displays "E: 8.395E+003"

Console.WriteLine("F: {0}",

integral.ToString("F01", ci)); // Displays "F: 8395.0"

Console.WriteLine("G: {0}",

integral.ToString("G", ci)); // Displays "G: 8395"

Console.WriteLine("N: {0}",

integral.ToString("N01", ci)); // Displays "N: 8,395.0"

Console.WriteLine("P: {0}",

(integral/10000).ToString("P02", ci)); // Displays "P: 83.95 %"

Console.WriteLine("X: 0x{0}",

integral.ToString("X", ci)); // Displays "X: 0x20CB"

Console.WriteLine();

}

}

Пример

В следующем примере целочисленное значение и числовое значение с плавающей запятой форматируются с помощью языка и региональных параметров en-us и всех спецификаторов стандартного числового формата. В этом примере используются два конкретных числовых типа, но схожие результаты будут получены и для любого из базовых числовых типов (Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Decimal, Single и Double).

-----------

Custom Numeric Format Strings

A custom numeric format string, which you create and consists of one or more custom numeric format specifiers, defines how numeric data is formatted. A custom numeric format string is equivalently defined as any string that is not a standard numeric format string.

The following table describes the custom numeric format specifiers. For more information, see the notes that follow the table.

Format specifier

Name

Description

0

Zero placeholder

If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the result string; otherwise, a '0' appears in the result string. The position of the leftmost '0' before the decimal point and the rightmost '0' after the decimal point determines the range of digits that are always present in the result string.

The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.

The following example displays several values formatted using custom format strings that include zero placeholders.