有时候double型数据需要toString(),但又想保留小数,当值为整数,比如3.00时tostring后会变为”3″,具体说明见下:
1 | string str0 = i.ToString( "f" ); //25.00 |
2 | string str1 = i.ToString( "f1" ); //25.0 |
3 | string str2 = i.ToString( "f2" ); //25.00 |
4 | string str3 = i.ToString( "f3" ); //25.000 |
5 | string str4 = i.ToString( "f4" ); //25.0000 |
其他方式的如下:
2 | string str0 = i.ToString( "n" ); //111,125.00 |
3 | string str1 = i.ToString( "n1" ); //111,125.0 |
4 | string str2 = i.ToString( "n2" ); //111,125.00 |
5 | string str3 = i.ToString( "n3" ); //111,125.000 |
6 | string str4 = i.ToString( "n4" ); //111,125.0000 |
如果还是不行,就要添加其他设置:
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
string str1 = i.hours.ToString("f1", provider);