简体   繁体   English

如何在C#中使用cultureinfo设置百万分隔符(')和千(。)

[英]How to set Million separator (') and thousand (.) using cultureinfo in C#

I'm trying to convert a value 9.999.999 to 9'999.999 using cultureinfo in C# but I can't get it works. 我正在尝试使用C#中的cultureinfo将值9.999.999转换为9'999.999,但我无法正常工作。

How can I do that? 我怎样才能做到这一点?

So it's a string and you want a different group-separator for first groups? 所以这是一个字符串,您要为第一组使用不同的组分隔符吗?

You could replace all but the last dot: 您可以替换除最后一个点以外的所有点:

string value = "9.999.999";
StringBuilder sb = new StringBuilder(value);
bool skippedLast = false;

for (int i = sb.Length - 1; i >= 0; i--)
{
    if (sb[i] == '.')
    {
        if (!skippedLast)
            skippedLast = true;
        else
            sb[i] = '\'';
    }
}

string desiredOutput = sb.ToString(); // 9'999.999

since the default IFormatProvider implementations that are shipped with the framework lack the capability you want/need, I suggest implementing the interface yourself 由于框架附带的默认IFormatProvider实现缺少您想要/需要的功能,因此建议您自己实现接口

additionally, since CultureInfo is not sealed, you can extend the class and provide your implementation to System.Threading.Thread.CurrentThread.CurrentCulture ... all consecutive formatting calls on that thread will use your implementation (unless called with a specific format provider...) 此外,由于CultureInfo没有密封,因此您可以扩展该类并将实现提供给System.Threading.Thread.CurrentThread.CurrentCulture ...该线程上的所有连续格式调用都将使用您的实现(除非使用特定的格式提供程序调用)。 ..)

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

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