C#Currency to string

Mar*_*iff 13 c#

我正在查询返回货币值的数据库字段,我将其分配给字符串,但最后会添加额外的00.

例如,查询返回 30.00

我将它分配给一个字符串(string value = Convert.ToString(ReturnValue);)但是当我输出它时,它显示为30.0000

你能告诉我哪里出错了,最好的办法是什么?

Sni*_*ers 14

MartGriff,

我最好的建议是使用SqlMoney类型将其转换为double.从那里,您可以根据需要输出它!

这是一个例子:

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");
Run Code Online (Sandbox Code Playgroud)

有关更多格式选项,请查看MSDN:

http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx

祝你好运,我希望这会有所帮助.


Ed *_* S. 9

您可以在ToString调用中使用字符串格式代码.


Ric*_*ett 6

你想要使用货币字符格式化你的字符串吗?

如果是这样...

decimal m = 3.4;

string s = string.Format("{0:c}", m);

// s will be £3.40, $3.40, etc depending on your locale settings
Run Code Online (Sandbox Code Playgroud)