Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Put spaces between numbers

Discussion in 'Scripting' started by Tutozz, Oct 16, 2018.

  1. Tutozz

    Tutozz

    Joined:
    Mar 16, 2018
    Posts:
    2
    Hello,
    I have a PlayerPref contain the money value and i want to know if it's possible to transform a int like that 1250000 to a string like that "1 250 000" simply in C#
    Thanks
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
  4. Tutozz

    Tutozz

    Joined:
    Mar 16, 2018
    Posts:
    2
    Thanks for your awnsers ;)
    This is my code:

    Code (CSharp):
    1.         Money = PlayerPrefs.GetInt("Money", 0);
    2.  
    3.         string MoneyPreArray = Money.ToString("N", CultureInfo.CreateSpecificCulture("fr-CA"));
    4.         int MoneyArray = MoneyPreArray.IndexOf(",");
    5.         if (MoneyArray > 0)
    6.             MoneyPreArray = MoneyPreArray.Substring(0, MoneyArray);
    7.  
    8.         MoneyCanvas.text =  MoneyPreArray + " $";
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    If you accept additional advice...

    Don't call it Array, it's not.

    Also it's the other way around, usually the $ is on the beginning of the expression ($200 and not 200 $).
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Damn it... I've been doing it wrong all these years!

    ...

    In all seriousness, though I'm familiar with it going on front. But I really have written 100$ and the sort for most of my life, if there's no cents, and I'm in conversation typing (like forums and messengers). Mainly because it reads "100 dollars".

    [edit]

    Just googled...

    It appears French-Canadians do put $ on the end. Maybe OP is French-Canadian?
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Probably the Canadians stole your soul and influenced your usage of the dollar symbol! :p :D

    AFAIK, grammatically, the proper way of using the $ is to put it in the front of the sum. In the US. Other English speaking countries use it (sometimes) on the other way around. Now, of course, it's not a big deal, since the language is changing and all, I was just a little bit overzealous here (also assumed that the target is the US-version).
     
  8. Radical_boy

    Radical_boy

    Joined:
    Jan 30, 2021
    Posts:
    8
    I have stumbled across this when i was trying to convert big numbers like 1234567 into 1 234 567. I have looked through String Formatting but found a much easier way to do this.

    Code (CSharp):
    1.  
    2. int i = 1234567;
    3. string s = i.ToString("#,#").Replace(',' , ' ');
    4.  
    5. //basically formats the number with comma after 3 digits then replaces the commas with spaces
    6.  
     
    octaav and julesaintho like this.