gpt4 book ai didi

c# - .NET 字符串中的货币符号放置

转载 作者:太空狗 更新时间:2023-10-29 22:34:40 24 4
gpt4 key购买 nike

我正在开发一些代码以在我的应用程序中将货币符号显示为标签的一部分,并且我有一个 Unicode 十六进制格式的货币符号引用列表。在我的代码中,我将货币格式化如下:

(货币符号)(十进制字符串)(货币描述)

这种方法适用于大多数符号,但我注意到有些符号会自动移到十进制值的右边,即使明确放在左边也是如此。使用调试器,我什至在字符串本身的最基本级别上看到了这种行为,因此这不是表示层中的渲染在更高级别上进行任何操作的情况。以下代码展示了演示问题的简单案例:

string rialSymbol = "\ufdfc";
string amount = "123.45";
string description = "Rials";
string plainConcat = rialSymbol + " " + amount + " " + description;
Debug.WriteLine(plainConcat);

调试输出(也与应用程序 UI 中看到的相匹配)如下:

123.45(里亚尔符号)里亚尔

(注意:符号位于小数点的右侧,而不是左侧,如指定的那样)

我尝试了多种方法和各种字符串格式、文化格式等,但似乎没有任何方法可以解决这个问题。在框架不决定相对于十进制值的符号位置的情况下,如何强制放置 unicode 字符?这适用于大多数其他字符,为什么里亚尔(和其他一些字符)会导致这种基本的字符串行为?

最佳答案

U+FDFC是一个从右到左的 Unicode 字符。它旨在嵌入从右到左的文本中。您正在混合从左到右和从右到左的文本。

来自 Wikipedia :

In Unicode encoding, all non-punctuation characters are stored in writing order. This means that the writing direction of characters is stored within the characters. If this is the case, the character is called "strong". Punctuation characters however, can appear in both LTR and RTL scripts. They are called "weak" characters because they do not contain any directional information. So it is up to the software to decide in which direction these "weak" characters will be placed. Sometimes (in mixed-directions text) this leads to display errors, caused by the bidi-algorithm that runs through the text and identifies LTR and RTL strong characters and assigns a direction to weak characters, according to the algorithm's rules.

In the algorithm, each sequence of concatenated strong characters is called a "run". A weak character that is located between two strong characters with the same orientation will inherit their orientation. A weak character that is located between two strong characters with a different writing direction, will inherit the main context's writing direction (in an LTR document the character will become LTR, in an RTL document, it will become RTL). If a "weak" character is followed by another "weak" character, the algorithm will look at the first neighbouring "strong" character. Sometimes this leads to unintentional display errors. These errors are corrected or prevented with "pseudo-strong" characters. Such Unicode control characters are called marks. The mark U+200E (left-to-right mark) or U+200F (right-to-left mark) is to be inserted into a location to make an enclosed weak character inherit its writing direction.

For example, to correctly display the U+2122 ™​ trade mark sign for an English name brand (LTR) in an Arabic (RTL) passage, an LRM mark is inserted after the trademark symbol if the symbol is not followed by LTR text. If the LRM mark is not added, the weak character ™ will be neighbored by a strong LTR character and a strong RTL character. Hence, in an RTL context, it will be considered to be RTL, and displayed in an incorrect order.

所以解决办法是在从右到左的货币符号后面加一个U+200E从左到右的标记:

string rialSymbol = "\ufdfc\u200e";
string amount = "123.45";
string description = "Rials";
string plainConcat = rialSymbol + " " + amount + " " + description;
Debug.WriteLine(plainConcat);

关于c# - .NET 字符串中的货币符号放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4272179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com