简体   繁体   English

如何根据货币正确设置文本格式

[英]How to correctly format text based on currency

In my application a calculation is performed which display the text to the GUI. 在我的应用程序中,执行了将文本显示到GUI的计算。 The application multiplies a user given amount by a defined number (Say 0.85) to create a total (User types in 2, application works out 2 x 0.85). 应用程序将用户给定的数量乘以定义的数字(说0.85)以创建总数(用户类型为2,应用程序得出2 x 0.85)。

As the number that is displayed is that of a currency I am trying to correctly format the text to make it readable and correct. 由于显示的数字是货币编号,因此我试图正确格式化文本以使其可读和正确。

So far I have tried 到目前为止,我已经尝试过

.ToString("N2");

This has just resulted in two additional zero's being appended to the end of the figure. 这只是导致在图的末尾附加了两个附加的零。

The problem can be seen here: 问题可以在这里看到:

错误!

As you can see the correct value is .68 (Or £0.68) and my text is showing £68.00 Taking the "N2" out of the ToString does help but I'm still left with £68. 如您所见,正确的值为.68(或£0.68),我的文字显示为£68.00,将“ N2”从ToString中取出确实有帮助,但我仍然剩下£68。

I know this is not as trivial as it sounds but it's something I've never needed to think about before and it's got me thinking about it for a long while. 我知道这并不像听起来那么琐碎,但这是我以前从未想过的事情,这让我思考了很长时间。

Thanks! 谢谢!

Note: The data is stored as a double and was previously a float, the application is flexible to change. 注意:数据存储为双精度数据,以前是浮动数据,因此可以灵活地更改应用程序。 The currency icon is also not needed as I am providing that manually, only the formatting is necessary. 货币图标也不需要,因为我是手动提供的,只需要格式化即可。

Try this: 尝试这个:

string.Format("{0:C}", money_value);

This will also work: 这也将起作用:

.ToString("C");

(I realize this will include the currency symbol, but the OP didn't say that was a problem, just that it wasn't necessary.) (我意识到这将包括货币符号,但操作员没有说这是一个问题,只是没有必要。)

If you want to go all out, you can do this: 如果您想全力以赴,可以这样做:

string.Format(ui_culture, “{0:C}”, money_value);

where ui_culture is the culture associated with the currency. 其中ui_culture是与货币关联的文化。

Edited to add: 编辑添加:

The good thing about this formatting is that it manages all your punctuation. 这种格式的好处是它可以管理所有标点符号。

I'm not sure if currency symbols are always the leading character. 我不确定货币符号是否始终是主角。 If so, you can remove it: 如果是这样,您可以将其删除:

string.Format(ui_culture, “{0:C}”, money_value).substring(1);

At first glance it looks like you've multiplied your value (0.68) by 100 to get 68.00 which would be correct. 乍一看,您似乎已将值(0.68)乘以100得到68.00,这是正确的。 However, your quantity appears to be 80 which should give you a value of 54.40. 但是,您的数量似乎是80,应该为您提供54.40的值。

If you are multiplying by 2 then you should get 1.70. 如果乘以2,则应该得到1.70。

You would need to use the ToString overload that takes an IFormatProvider paramter: 您将需要使用带有IFormatProvider参数的ToString重载:

double value = 80;
string ukCurrency = value.ToString("N2", CultureInfo.CreateSpecificCulture("en-GB"));

I am actually not sure if this will include the currency character (untested example). 我实际上不确定是否会包含货币字符(未经测试的示例)。 I am hoping that the use of the "N2" format string will strip the currency character...but it may not. 我希望使用“ N2”格式的字符串会去除货币字符……但可能不会。 It might be easy enough to skip the first character of the string: 跳过字符串的第一个字符可能很容易:

ukCurrency = ukCurrency.Substring(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM