我正在查询一个返回货币值的数据库字段,我将其赋值给一个字符串,但它在末尾添加了额外的00。
例如,查询返回30.00
我将它赋给一个字符串(string value = Convert.ToString(ReturnValue);),但是当我输出它时,它显示为30.0000
你能告诉我我哪里错了吗?最好的解决方法是什么?
发布于 2009-03-03 23:25:22
您可以在ToString调用中使用string format codes。
发布于 2009-03-03 23:58:40
MartGriff,
我最好的建议是使用SqlMoney类型将其转换为双精度型。在那里,你可以随心所欲地输出它!
下面是一个例子:
System.Data.SqlTypes.SqlMoney ReturnValue;
//Set your returnValue with your SQL statement
ReturnValue = ExecuteMySqlStatement();
//Get the format you want
//$30.00
string currencyFormat = ReturnValue.ToDouble().ToString("c");
//30.00
string otherFormat = ReturnValue.ToDouble().ToString("0.00");有关更多格式选项,请查看MSDN:
http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx
祝你好运,我希望这对你有帮助。
发布于 2009-03-03 23:33:50
是否要使用货币字符格式化字符串?
如果是的话..。
decimal m = 3.4;
string s = string.Format("{0:c}", m);
// s will be £3.40, $3.40, etc depending on your locale settingshttps://stackoverflow.com/questions/608667
复制相似问题