1

I am trying to format an int with decimal points as separators, not commas.

Example: 1234567890 should be formatted to 1.234.567.890

text_team1.text = em.team1Score.ToString("#,##0");

This will give 1,234,567,890

However in this topic there was some information about using the class CultureInfo which contains the format-style, so I used several of them:

text_team1.text = em.team1Score.ToString("#,##0", new System.Globalization.CultureInfo("IS-is"));

as an example. But it seems that every cultureInfo uses a comma as separator.

Even by editing the string afterwards, there is still the comma as seperator.

text_team1.text = em.team1Score.ToString("#,##0");
text_team1.text.Replace(',','.');
Community
  • 1
  • 1
Mc Midas
  • 189
  • 1
  • 5
  • 17
  • The globalized approach varies by .... culture. Some places use spaces for separators (`1 234 567 890)`, others use commas (`1,234,567,890`), others do different things. The globalize approach should only be used if you want to show the use a number in their native format. If you're doing something custom (e.g., a dot seperated ip address), you should not use it. It seems that you're trying to show the score to a user, so you should use their native format by using the globalized string. – Alexander Mar 08 '17 at 23:19
  • Interestingly, CultureInfo "IS-is" does use the decimal separator in dotnetfiddle. Here's an example https://dotnetfiddle.net/KqCmlv – DeanOC Mar 08 '17 at 23:33

3 Answers3

2

Even by editing the string afterwards, there is still the comma as seperator.

text_team1.text = em.team1Score.ToString("#,##0");
text_team1.text.Replace(',','.');

You forgot to assign the replaced string back.

text_team1.text = text_team1.text.Replace(',','.');

EDIT:

If you still prefer a solution without using the Replace function, you can use the Extension method below. It works for strings and ints. Please Google and read about extension methods if you don't know how they work.

Create and place the ExtensionMethod script in any folder in your project:

using System.Globalization;
using System;

public static class ExtensionMethod
{
    public static string formatStringWithDot(this string stringToFormat)
    {
        string convertResult = "";
        int tempInt;
        if (Int32.TryParse(stringToFormat, out tempInt))
        {
            convertResult = tempInt.ToString("N0", new NumberFormatInfo()
            {
                NumberGroupSizes = new[] { 3 },
                NumberGroupSeparator = "."
            });
        }
        return convertResult;
    }

    public static string formatStringWithDot(this int intToFormat)
    {
        string convertResult = "";

        convertResult = intToFormat.ToString("N0", new NumberFormatInfo()
        {
            NumberGroupSizes = new[] { 3 },
            NumberGroupSeparator = "."
        });
        return convertResult;
    }
}

Usage:

string stringToFormat = "1234567890";
Debug.Log(stringToFormat.formatStringWithDot());

Or

int intToFormat = 1234567890;
Debug.Log(intToFormat.formatStringWithDot());

Or

string stringToFormat = "1234567890";
text_team1.text = stringToFormat.formatStringWithDot();

Use each one depending on which scenario you run into.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • What a fail! Thank you! __text_team1.text = em.team1Score.ToString("#,##0").Replace(',','.')__ much shorter :D – Mc Midas Mar 08 '17 at 23:23
2

If you are using globalisation as a means to format your string, you could set a Custom Group Seperator

NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;

// Displays the same value with a blank as the separator.
Int64 myInt = 1234567890;
nfi.NumberGroupSeparator = ".";
Console.WriteLine( myInt.ToString( "N0", nfi ) );

https://dotnetfiddle.net/vRqd6x

Libin Varghese
  • 1,506
  • 13
  • 19
2

I prefer using string.Format(). Does your OS represent numbers with that format? If so, you can try with the simplest form

text_team1.text = string.Format("{0:N}", em.team1Score);

or you can force it with

text_team1.text = string.Format(CultureInfo.GetCultureInfo("IS-is"), "{0:N}", em.team1Score);

which cares of the culture.

Kroneaux Schneider
  • 264
  • 1
  • 2
  • 13