标准数字格式

date
Dec 4, 2021
slug
10018
status
Published
tags
C#
summary
type
Post
{index[,alignment][:formatString]},例如{0,10:F5}
// 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.0).ToString("P02", ci)); // Displays "P: 83.95 %"
Console.WriteLine("X: 0x{0}", integral.ToString("X", ci));           // Displays "X: 0x20CB"
Console.WriteLine();
 
 
LogTextBox.AppendText(Environment.NewLine + "All badChan count");
LogTextBox.AppendText(string.Format("{0}{1}{2,10:P4}",Environment.NewLine , badChanCount, badChanCount1.0/(allSPallChan)));
LogTextBox.AppendText(Environment.NewLine + "All SP count");
LogTextBox.AppendText(string.Format("{0}{1}{2,10:P4}", Environment.NewLine, badSPCount, badSPCount * 1.0 / allSP));
LogTextBox.AppendText(Environment.NewLine + "Flag=2 count");
LogTextBox.AppendText(string.Format("{0}{1}{2,10:P4}", Environment.NewLine, flag2Count, flag2Count * 1.0 / (allSP*allChan)));
LogTextBox.AppendText(Environment.NewLine + "Flag=3 count");
LogTextBox.AppendText(string.Format("{0}{1}{2,10:P4}", Environment.NewLine, flag3Count, flag3Count * 1.0 / (allSP * allChan)));
notion image
notion image
Standard Numeric Format Specifiers
Name and Characters
Meaning
Formats the value as a currency, using a currency symbol. The currency symbol used will depend on the culture setting of the PC running the program. Precision specifier: The number of decimal places. Sample: Console.WriteLine("{0 :C}", 12.5); Output: $12.50
A string of decimal digits, with a negative sign, if appropriate. Can be used only with integer types. Precision specifier: The minimum number of digits to use in the output string. If the number has fewer digits, it will be padded with 0s on the left.
A string of decimal digits with a decimal point. Can also include a negative sign, if appropriate. Precision specifier: The number of decimal places. Sample: Console.WriteLine("{0 :F4}", 12.3456789); Output: 12.3457
A compact fixed-point representation or a scientific notation representation, depending on the value. This is the default, if no specifier is listed. Precision specifier: Depends on the value. Sample: Console.WriteLine("{0 :G4}", 12.3456789); Output: 12.35
A string of hexadecimal digits. The hex digits A through F will match the case of the specifier. Precision specifier: The minimum number of digits to use in the output string. If the number has fewer digits, it will be padded with 0s on the left. Sample: Console.WriteLine("{0 :x}", 180026); Output: 2bf3a
Similar to fixed-point representation but includes comma or period separators between each group of three digits, starting at the decimal point and going left. Whether it uses a comma or a period depends on the culture setting of the PC running the program. Precision specifier: The number of decimal places. Sample: Console.WriteLine("{0 :N2}", 12345678.54321); Output: 12,345,678.54
A string that represents percent. The number is multiplied by 100. Precision specifier: The number of decimal places. Sample: Console.WriteLine("{0 :P2}", 0.1221897); Output: 12.22 %
The output string is chosen so that if the string is converted back to a numeric value using a Parse method, the result will be the original value. Parse methods are described in Chapter 27. Precision specifier: Ignored. Sample: Console.WriteLine("{0 :R}", 1234.21897); Output: 1234.21897
Scientific notation with a mantissa and an exponent. The exponent is preceded by the letter E. The E character will be the same case as the specifier. Precision specifier: The number of decimal places. Sample: Console.WriteLine("{0 :e4}", 12.3456789); Output: 1.2346e+001
 

© Wen Bo 2021 - 2022