简体   繁体   中英

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.

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

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...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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