`
holoblog
  • 浏览: 1228570 次
博客专栏
E0fcf0b7-6756-3051-9a54-90b4324c9940
SQL Server 20...
浏览量:18940
文章分类
社区版块
存档分类
最新评论

C#数字格式

 
阅读更多

usingSystem;

classFormattingNumbers
{
staticvoidMain()
{
decimaltheDecNumber=12345.678m;//the"m"createsaliteraloftypedecimalfromadouble
//UsingtheToStringMethod
//thenumberintheformatstringistheprecisionspecifier
Console.WriteLine("Noformatting:"+theDecNumber.ToString());
Console.WriteLine(
"Currencyformatting:"+theDecNumber.ToString("C"));
Console.WriteLine(
"Exponentialformatting:"+theDecNumber.ToString("E"));
Console.WriteLine(
"Fixed-pointformatting:"+theDecNumber.ToString("F2"));
Console.WriteLine(
"Generalformatting:"+theDecNumber.ToString("G"));
Console.WriteLine(
"Numberformattingto2decimalplaces:"+theDecNumber.ToString("N2"));
Console.WriteLine(
"Numberformattingto3decimalplaces:"+theDecNumber.ToString("N3"));
Console.WriteLine(
"Numberformattingto4decimalplaces:"+theDecNumber.ToString("N4"));
Console.WriteLine(
"Percentformatting:"+theDecNumber.ToString("P0"));

inttheIntNumber=123456;
Console.WriteLine(
"Hexidecimalformatting(forintegers):{0}={1}",theIntNumber,theIntNumber.ToString("X"));

doubletheDblNumber=1234567890;
Console.WriteLine(
"Customformatting:{0}toUStelephone{1}",theDblNumber,theDblNumber.ToString("(###)###-####"));

//Keepconsoleopenifnotrunthroughcommandprompt
Console.Write("/nPressEntertoContinue");
Console.ReadLine();
}

}

N2表示小数点后保留2位....

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics