C# 将数字格式化为钱

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/820913/
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-05 02:29:47  来源:igfitidea点击:

Format number as money

c#stringformat

提问by Alex

How do I format a number to look like this: 9,000 my database field is in money data type, when I pull it up I see it like this: 9000.0000 that don't look right to me (I would like it to look like a real money format)

如何将数字格式化为如下所示: 9,000 我的数据库字段是货币数据类型,当我将其拉起时,我看到它是这样的: 9000.0000 对我来说看起来不合适(我希望它看起来像真钱格式)

回答by Jon

decimal money = 1000;
Response.Write(string.Format("{0:C}", money));

The above is an example in C#.

以上是C#中的一个例子。

回答by Travis

You need to do a "formatting" function (in whatever language you're using) that will return the data in a currency format. Another answer already describes the code in C#. Here's vbscript:

您需要执行一个“格式化”函数(以您使用的任何语言),以货币格式返回数据。另一个答案已经描述了 C# 中的代码。这是 vbscript:

Dim Amount
Amount = 2000
Response.Write(FormatCurrency(Amount))

How your database viewing application reads the data and how it is actually stored in the database may be two different things.

您的数据库查看应用程序如何读取数据以及它如何实际存储在数据库中可能是两种不同的事情。

回答by Powerlord

While you could call string.format, I think it's easier to just call ToStringon it.

虽然您可以调用 string.format,但我认为在其上调用ToString更容易。

decimal money = 9000m;
string formattedMoney = money.ToString("C");

回答by DPenner1

Thought I'd add a C# 6 option with interpolated strings:

以为我会添加一个带有内插字符串的 C# 6 选项:

decimal money = 9000m;
string formatted = $"{money:C}";