C# 货币到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/608667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:10:38  来源:igfitidea点击:

C# Currency to string

c#

提问by MartGriff

I am querying a database field that returns a money value, I am assigning this to a string but it is adding extra 00 on the end.

我正在查询一个返回货币值的数据库字段,我将它分配给一个字符串,但它在末尾添加了额外的 00。

e.g. Query returns 30.00

例如查询返回 30.00

I assign this to a string (string value = Convert.ToString(ReturnValue);) but when I output it, it is showing as 30.0000

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

Can you advise me where I'm going wrong and the best way to approach this?

你能告诉我我哪里出错了以及解决这个问题的最佳方法吗?

采纳答案by Ed S.

You can use string format codesin your ToString call.

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

回答by Jay Riggs

Try this:

尝试这个:

yourValueHere.ToString("0.00")

回答by Scott Ferguson

I'd use something like

我会使用类似的东西

string value = ReturnValue.ToString("0.00");

This uses the ToString overload that accepts a format string. The above format string "0.00" specifies two decimal places.

这使用接受格式字符串的 ToString 重载。上述格式字符串“0.00”指定了两位小数。

回答by Richard Ev

Do you want your string formatted using a currency character?

您想使用货币字符格式化字符串吗?

If so...

如果是这样的话...

decimal m = 3.4;

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

// s will be £3.40, .40, etc depending on your locale settings

回答by bstoney

In SQLServer a money data type is the equivalent of a decimal with 4 decimal places of precision. I would assume this is precision is conserved when the decimal is converted to a string. The best option would be to always use a format string eg "#,##0.00" when doing the conversion.

在 SQLServer 中,货币数据类型相当于具有 4 个小数位精度的小数。我认为这是在将小数转换为字符串时保留精度。最好的选择是在进行转换时始终使用格式字符串,例如“#,##0.00”。

回答by Ian Suttle

The Money data type has a precision of 4 decimal places. You'll need to either specify the format in the ToString() arguments or you can round off the value before converting to a string.

Money 数据类型的精度为 4 位小数。您需要在 ToString() 参数中指定格式,或者您可以在转换为字符串之前对值进行四舍五入。

If you use .ToString() you're not getting any rounding as far as I know. I think you just lose the digits.

如果你使用 .ToString() 就我所知,你不会得到任何四舍五入。我想你只是失去了数字。

If you round, you're not just chopping off digits. Here's a sample for rounding (untested code):

如果你四舍五入,你不仅仅是砍掉数字。这是一个四舍五入示例(未经测试的代码):

string value = Math.Round(ReturnValue, 2).ToString();

回答by Snickers

MartGriff,

马特格里夫,

My best advice would be to convert it to a double using the SqlMoney type. From there, you can output it however you would like!

我最好的建议是使用 SqlMoney 类型将其转换为双精度型。从那里,您可以随意输出它!

Here's an example:

下面是一个例子:

System.Data.SqlTypes.SqlMoney ReturnValue;

//Set your returnValue with your SQL statement
ReturnValue = ExecuteMySqlStatement();

//Get the format you want

//.00
string currencyFormat = ReturnValue.ToDouble().ToString("c");

//30.00
string otherFormat = ReturnValue.ToDouble().ToString("0.00");

For more formatting options, check out the MSDN:

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

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

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

Best of luck, I hope this helps.

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

回答by Anthony Xavier

I am currently developing a boatdock web system, using VS 2013 Community edition. I was trying to work out how to output a currency value into a textbox.

我目前正在开发一个船坞网络系统,使用 VS 2013 社区版。我试图找出如何将货币值输出到文本框中。

The method that I used was

我使用的方法是

fieldName.Text = decimalValue.ToString("c");

fieldName.Text = decimalValue.ToString("c");

It works perfectly.

它完美地工作。

回答by Ali Nassajpour

My recommendation is to convert it to Decimal firstly. I'd use something like this:

我的建议是先将其转换为十进制。我会使用这样的东西:

string value = (Convert.ToDecimal(ReturnValue)).ToString("0,0.00");

回答by New_developer

string value = (Convert.ToDecimal(ReturnValue)).ToString("c");

c is for currency p for percentage. There is a list of other lettersyou can use.

c 是货币 p 的百分比。有一个您可以使用的其他字母列表