c#保留小数点后位数的方法
Double dValue = 95.12345;
int iValue = 10000;
string strValue = "95.12345";
string result = "";
result = Convert.ToDouble(dValue).ToString("0.00");//保留小数点后两位,结果为95.12
result = Convert.ToDouble(iValue).ToString("0.00");//10000.00
result = Convert.ToDouble(strValue).ToString("0.00");//95.12
result = Convert.ToDouble(dValue).ToString("P");//得到小数点后2位的百分比,自动 加上%号;//9512.35%
result = Convert.ToDouble(strValue).ToString("f4");//保留小数点后4位; //95.1235
//要注意的一点是 Convert.ToDouble一定要是这种双精度的,不然会报错。
DateTime dt =New Date();
dt.ToString("yy-MM-dd hh:mm:ss");
在数库里作换转:select convert(char(11),GETDATE(),23)
Double dValue = 95.12345;
int iValue = 10000;
string strValue = "95.12345";
string result = "";
result = Convert.ToDouble(dValue).ToString("0.00");//保留小数点后两位,结果为95.12
result = Convert.ToDouble(iValue).ToString("0.00");//10000.00
result = Convert.ToDouble(strValue).ToString("0.00");//95.12
result = Convert.ToDouble(dValue).ToString("P");//得到小数点后2位的百分比,自动 加上%号;//9512.35%
result = Convert.ToDouble(strValue).ToString("f4");//保留小数点后4位; //95.1235
//要注意的一点是 Convert.ToDouble一定要是这种双精度的,不然会报错。
DateTime dt =New Date();
dt.ToString("yy-MM-dd hh:mm:ss");
在数库里作换转:select convert(char(11),GETDATE(),23)
本文介绍了使用C#语言保留数值小数点后位数的方法,包括双精度值、整数值和字符串转换。同时,演示了如何将日期格式化为指定格式。

1万+

被折叠的 条评论
为什么被折叠?



