0

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
David López
  • 366
  • 3
  • 12

2 Answers2

2

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
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

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

DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
  • You also need the `ICustomFormatter`. Perhaps you want to add that to your answer. See: https://msdn.microsoft.com/en-us/library/system.icustomformatter(v=vs.110).aspx – Martin Mulder Jun 22 '17 at 20:59