使用某些货币符号时自定义数字格式和字符串替换期间出现意外结果

发布于 2024-11-05 02:14:14 字数 636 浏览 2 评论 0原文

我需要以以下格式显示所有货币:

"[Code] [Symbol]#,##0.00;[Code] [Symbol](#,##0.00)"

用户将选择一个国家/地区,我从外部服务获取货币代码和符号。然后,我使用 string.replace 将货币代码和符号设置为上面的格式字符串。

因此,值 123.456(美元)的格式如下:

"USD $123.46"

此方法适用于大多数货币符号,但我对科威特 ð.ك 的货币符号有疑问。我似乎无法格式化任何值以允许符号出现在数字之前。我对字母没有同样的问题。我认为这与阿拉伯语是从右向左书写的事实有关。该符号甚至在浏览器中也有同样的行为。我无法从左到右选择它,选择从右侧开始。 另外,当我在 linqpad 中进行测试时,我的字符串显示为 [Kuwait_Symbol]#,##0.00,但是粘贴到 stackoverflow 文本框中会导致该字符串变为 ð.ك#,## 0.00

请注意,字符串中字符的顺序已更改。

不过,我有一种感觉,我不得不建议使用符号是不可行的,有什么办法可以解决这个问题吗?另外,为什么符号不能显示在数字之前?

I have a requirement to display all currencies in the following format:

"[Code] [Symbol]#,##0.00;[Code] [Symbol](#,##0.00)"

The user will chose a country and I get the currency code and symbol from an external service. I then use string.replace to set the currency code and symbol into the format string above.

So the value 123.456 (US Dollars) would be formatted as follows:

"USD $123.46"

This approach works for most currency symbols but I am having a problem with the currency symbol for Kuwait د.ك. I cannot seem to format any value to allow the symbol to appear before the numbers. I don't have the same problem with letters. I am thinking that this has to do with the fact that Arabic is written from right to left. The symbol even behaves that way in browser. I can't select it from left to right, selection starts from the right.
Also when I was testing in linqpad, my string was displayed as [Kuwait_Symbol]#,##0.00, however pasting in the stackoverflow textbox caused the string to be د.ك#,##0.00

Note that the ordering of the characters in the string has changed.

Though, I have a feeling that I will have to recommend that using symbols are not feasible, is there any way to get around this problem? Also, why can't the symbol be displayed before numbers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-11-12 02:14:14

该问题源于浏览器以及它们如何处理从右到左语言中的字符,而不是生成它们的代码。如果您尝试,

var kuwaitCulture = new System.Globalization.CultureInfo("ar-KW");
Response.Write(Server.UrlEncode(amount.ToString("c", kuwaitCulture)));

您会发现返回的内容清楚地在金额之前有CurrencySymbol。

要解决此问题,您可以使用相应的 Unicode 字符在浏览器中强制从左到右呈现。例如,

Response.Write('\u202A' + kuwaitCulture.NumberFormat.CurrencySymbol + '\u202C' + amount);

按照您的规范正确呈现。

The issue stems from browsers and how they handle characters in right-to-left languages rather than the code that generates them. If you try

var kuwaitCulture = new System.Globalization.CultureInfo("ar-KW");
Response.Write(Server.UrlEncode(amount.ToString("c", kuwaitCulture)));

you'll see that what gets returned clearly has the CurrencySymbol before the amount.

To get around this you can force left-to-right rendering in the browser with the corresponding Unicode character. For example,

Response.Write('\u202A' + kuwaitCulture.NumberFormat.CurrencySymbol + '\u202C' + amount);

renders correctly as per your specification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文