using System;
using System.Collections.Generic;
using System.Text;
namespace Text
{
class Program
{
static void Main(string[] args)
{
//1.货币格式
double k = 1234.5678;
Console.WriteLine("HelloWorld");
Console.WriteLine("{0,8:c}",k);//输出¥1234.57
Console.WriteLine("{0,10:c4}", k);//输出¥1234.5678
//2.整数数据类型格式
int num = 1234;
Console.WriteLine("{0:D}",num);//输出1234
Console.WriteLine("{0:d3}", num);//输出1234
Console.WriteLine("{0:d5}", num);//输出01234
//3.科学技数法格式
int num1 = 123000;
double f = 1234.5678;
Console.WriteLine("{0:E}",num1);//输出1.230000E+005
Console.WriteLine("{0:e}", num1);//输出1.230000e+005
Console.WriteLine("{0:E}", f);//输出1.234568E+003
Console.WriteLine("{0:e}", f );//输出1.234568e+005
Console.WriteLine("{0:e4}", f);//输出1.2346e+003
Console.WriteLine("{0:e4}", num1);//输出1.2300E+005
Console.WriteLine("**********");
//4.浮点数据格式
Console.WriteLine("{0,-8:f}", num1);//输出123000.00
Console.WriteLine("{0:f}", f);//输出12345.56
Console.WriteLine("{0,-8:f4}", num1);//输出123000.0000
Console.WriteLine("{0:f3}", f);//输出1234.568
Console.WriteLine("{0:f6}", f);//输出1234.56800
//5.通用格式
double k1 = 1234.789;
int j = 123456;
Console.WriteLine("{0:g}",j);//输出123456
Console.WriteLine("{0:g}", k1);//输出1234.789
Console.WriteLine("{0:g4}", k1);//输出1235
Console.WriteLine("{0:g4}", j);//输出1.235e+05
Console.WriteLine("*********");
//6.自然数据格式
double m = 211122.12345;
int o = 1234567;
Console.WriteLine("{0:N}", m);//输出211,122,.12
Console.WriteLine("{0:n}", o);//输出1,234,567.00
Console.WriteLine("{0:n4}", m);//输出211,122.1235
Console.WriteLine("{0:n4}", o);//输出1,234,567.0000
//7.十六进制数据格式
int l = 123456;
Console.WriteLine("{0:x}", l);//输出1e240
Console.WriteLine("{0:x6}", l);//输出01e240
}
}
}
C# 8个常用的格式字符
最新推荐文章于 2023-01-03 20:32:55 发布