且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.NET格式小数到两个地方或一个整数

更新时间:2022-12-09 17:12:25

 十进制NUM = 10.11M;

Console.WriteLine(num.ToString()0 ##);
 

For 10 I want 10 and not 10.00 For 10.11 I want 10.11

Is this possible without code? i.e. by specifying a format string alone simlar to {0:N2}

Thanks!

decimal num = 10.11M;

Console.WriteLine( num.ToString( "0.##" ) );