可以用ToString()方法来格式化数字的格式
具体可在MSDN中搜索“零占位符”。
Code
double value;
value = 123;
Console.WriteLine(value.ToString("00000"));
// Displays 00123
value = 1.2;
Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
Console.WriteLine(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20
value = .56;
Console.WriteLine(value.ToString("0.0", CultureInfo.InvariantCulture));
// Displays 0.6
value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("#,#",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 1.234.567.890
double value;
value = 1.2;
Console.WriteLine(value.ToString("#.##", CultureInfo.InvariantCulture));
// Displays 1.2
value = 123;
Console.WriteLine(value.ToString("#####"));
// Displays 123
value = 123456;
Console.WriteLine(value.ToString("[##-##-##]"));
// Displays [12-34-56]
value = 1234567890;
Console.WriteLine(value.ToString("#"));
// Displays 1234567890
Console.WriteLine(value.ToString("(###) ###-####"));
// Displays (123) 456-7890
double value;
value = 1.2;
Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
Console.WriteLine(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20
value = .086;
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture));
// Displays 8.6%
value = 86000;
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
// Displays 8.6E+4
double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235
double value = 1234567890;
Console.WriteLine(value.ToString("#,,", CultureInfo.InvariantCulture));
// Displays 1235
Console.WriteLine(value.ToString("#,,,", CultureInfo.InvariantCulture));
// Displays 1
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235
double value = .086;
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture));
// Displays 8.6%
double value = 86000;
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
// Displays 8.6E+4
Console.WriteLine(value.ToString("0.###E+000", CultureInfo.InvariantCulture));
// Displays 8.6E+004
Console.WriteLine(value.ToString("0.###E-000", CultureInfo.InvariantCulture));
// Displays 8.6E004
double posValue = 1234;
double negValue = -1234;
string fmt = "##;(##)";
Console.WriteLine(posValue.ToString(fmt)); // Displays 1234
Console.WriteLine(negValue.ToString(fmt)); // Displays (1234)
double value;
value = 123;
Console.WriteLine(value.ToString("00000"));
// Displays 00123
value = 1.2;
Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
Console.WriteLine(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20
value = .56;
Console.WriteLine(value.ToString("0.0", CultureInfo.InvariantCulture));
// Displays 0.6
value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("#,#",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 1.234.567.890
double value;
value = 1.2;
Console.WriteLine(value.ToString("#.##", CultureInfo.InvariantCulture));
// Displays 1.2
value = 123;
Console.WriteLine(value.ToString("#####"));
// Displays 123
value = 123456;
Console.WriteLine(value.ToString("[##-##-##]"));
// Displays [12-34-56]
value = 1234567890;
Console.WriteLine(value.ToString("#"));
// Displays 1234567890
Console.WriteLine(value.ToString("(###) ###-####"));
// Displays (123) 456-7890
double value;
value = 1.2;
Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
Console.WriteLine(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20
value = .086;
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture));
// Displays 8.6%
value = 86000;
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
// Displays 8.6E+4
double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235
double value = 1234567890;
Console.WriteLine(value.ToString("#,,", CultureInfo.InvariantCulture));
// Displays 1235
Console.WriteLine(value.ToString("#,,,", CultureInfo.InvariantCulture));
// Displays 1
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235
double value = .086;
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture));
// Displays 8.6%
double value = 86000;
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
// Displays 8.6E+4
Console.WriteLine(value.ToString("0.###E+000", CultureInfo.InvariantCulture));
// Displays 8.6E+004
Console.WriteLine(value.ToString("0.###E-000", CultureInfo.InvariantCulture));
// Displays 8.6E004
double posValue = 1234;
double negValue = -1234;
string fmt = "##;(##)";
Console.WriteLine(posValue.ToString(fmt)); // Displays 1234
Console.WriteLine(negValue.ToString(fmt)); // Displays (1234)