• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by camlam999 · Oct 05, 2016 at 10:01 AM · c#format

How to convert/format numbers? (eg. 123K, 456M, 789B)

a simple way to format numbers?

1000 - 1K 1000000 - 10M

you get it, sorry this is so little but im a little annoyed since i just wrote a paragraph and a half but when attempting to post it erased it all....

can someone help me out with this and perhaps explain what it all means my knowledge of C# next to useless

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fct509 · Apr 04, 2019 at 03:32 AM 0
Share

I know what you mean about the post getting erased after spending a ton of time working on it. I've had a few posts get erased after spending close to half an hour getting rid of spelling mistakes (which I'm very prone to) and other errors. Some times, I wait a minute and then re-read my post because I can think faster than I can type, and I sometimes leave out a word or two just to keep up with my thoughts. A few times, the word that gets skipped is something small like "not", which can completely change the question I'm asking. So, yeah, I feel your pain.

avatar image alikun · Sep 06, 2019 at 09:24 AM 0
Share

Tried to make as short as possible, came up with this:

 `private static List<Tuple<int, string>> ZeroesAndLetters = new List<Tuple<int, string>>()
 {
     new Tuple<int, string>(15, "Q"),
     new Tuple<int, string>(12, "T"),
     new Tuple<int, string>(9, "B"),
     new Tuple<int, string>(6, "$$anonymous$$"),
     new Tuple<int, string>(3, "$$anonymous$$"),
 };

 public static string GetPointsShortened(ulong num)
 {
     int zeroCount = num.ToString().Length;
     for (int i = 0; i < ZeroesAndLetters.Count; i++)
         if (zeroCount >= ZeroesAndLetters[i].Item1)
             return (num / $$anonymous$$ath.Pow(10, ZeroesAndLetters[i].Item1)).ToString() + ZeroesAndLetters[i].Item2;
     return num.ToString();
 }`

In the ZeroesAndLetters list just add the numbers you need.

15, 12, 9 and ... are the number of zeroes, and Q, T, B and ... are shortened names.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mindmapfreak · Oct 05, 2016 at 10:51 AM

The following code should do what you want. You can easily add additional abbrevations.

 using UnityEngine;
 using System.Collections.Generic;
 using System.Linq;
 
 public class Test : MonoBehaviour
 {    
     void Start()
     {
         //Some tests
         Debug.Log("123000 is "+ AbbrevationUtility.AbbreviateNumber(123000));
         Debug.Log("456000000 is " + AbbrevationUtility.AbbreviateNumber(456000000));
         Debug.Log("789000000000 is " + AbbrevationUtility.AbbreviateNumber(789000000000));
         Debug.Log("1000 is " + AbbrevationUtility.AbbreviateNumber(1000));
         Debug.Log("1000000 is " + AbbrevationUtility.AbbreviateNumber(1000000));
     }    
 }
 
 public static class AbbrevationUtility
 {
     private static readonly SortedDictionary<int, string> abbrevations = new SortedDictionary<int, string>
     {
         {1000,"K"},
         {1000000, "M" },
         {1000000000, "B" }
     };
 
     public static string AbbreviateNumber(float number)
     {
         for (int i = abbrevations.Count - 1; i >= 0; i--)
         {
             KeyValuePair<int, string> pair = abbrevations.ElementAt(i);
             if (Mathf.Abs(number) >= pair.Key)
             {
                 int roundedNumber = Mathf.FloorToInt(number / pair.Key);
                 return roundedNumber.ToString() + pair.Value;
             }
         }
         return number.ToString();
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image camlam999 · Oct 05, 2016 at 02:21 PM 0
Share

Got it working, only problem i have now is making it pick up past billions, i set the score as a float so it would pass 2 billion but now get problems while trying to create T (Trillion) as 1000000000000 and got The best overloaded collection initalizer method System.Collections.Generic.SortedDictionary<int,string>.Add(int, string)' has some invalid arguments as well as Argument #1' cannot convert long' expression to type int' also tried switching ints to floats but that just created more errors :(

avatar image Mindmapfreak camlam999 · Oct 05, 2016 at 04:55 PM 0
Share

The maximum possible value for int is 2,147,483,647. You have to change int to long if you want to work with larger numbers. The maximum value for long is 9,223,372,036,854,775,807.

 using UnityEngine;
 using System.Collections.Generic;
 using System.Linq;
 
 public class Test : $$anonymous$$onoBehaviour
 {
     void Start()
     {
         //Some tests
         Debug.Log("123000 is " + AbbrevationUtility.AbbreviateNumber(123000));
         Debug.Log("456000000 is " + AbbrevationUtility.AbbreviateNumber(456000000));
         Debug.Log("789000000000 is " + AbbrevationUtility.AbbreviateNumber(789000000000));
         Debug.Log("1000 is " + AbbrevationUtility.AbbreviateNumber(1000));
         Debug.Log("1000000 is " + AbbrevationUtility.AbbreviateNumber(1000000));
         Debug.Log("135000000000000 is " + AbbrevationUtility.AbbreviateNumber(135000000000000));
     }
 }
 
 public static class AbbrevationUtility
 {
     private static readonly SortedDictionary<long, string> abbrevations = new SortedDictionary<long, string>
     {
         {1000,"$$anonymous$$"},
         {1000000, "$$anonymous$$" },
         {1000000000, "B" },
         {1000000000000,"T"}
     };
 
     public static string AbbreviateNumber(float number)
     {
         for (int i = abbrevations.Count - 1; i >= 0; i--)
         {
             $$anonymous$$eyValuePair<long, string> pair = abbrevations.ElementAt(i);
             if ($$anonymous$$athf.Abs(number) >= pair.$$anonymous$$ey)
             {
                 int roundedNumber = $$anonymous$$athf.FloorToInt(number / pair.$$anonymous$$ey);
                 return roundedNumber.ToString() + pair.Value;
             }
         }
         return number.ToString();
     }
 }
avatar image camlam999 · Oct 05, 2016 at 11:04 PM 0
Share

how the hell does cookie clicker go so high O_o either way thanks for your help :)

avatar image camlam999 · Oct 06, 2016 at 02:49 AM 0
Share

Still some bugs :P past B so T and Q it has to pass 1000000028671 for T and 1000000020545535 for Q.... so even 1 above either of those will bring it to 1T or 1Q but otherwise 1000B and 1000T... kinda annoying but thats just unity i guess :/

avatar image TheKuscu · Oct 30, 2018 at 02:49 PM 0
Share

@$$anonymous$$indmapfreak How about 12.5$$anonymous$$ or 987.5$$anonymous$$ ??

avatar image Kiragan TheKuscu · Apr 03, 2019 at 02:22 PM 0
Share

private static readonly SortedDictionary abbrevations = new SortedDictionary { {1000,"$$anonymous$$"}, {1000000, "$$anonymous$$" }, {1000000000, "B" } };

 public static string Format$$anonymous$$(float number)
 {
     for (int i = abbrevations.Count - 1; i >= 0; i--)
     {
         $$anonymous$$eyValuePair<int, string> pair = abbrevations.ElementAt(i);
         if ($$anonymous$$athf.Abs(number) >= pair.$$anonymous$$ey)
         {

             float rest = number % pair.$$anonymous$$ey;
             float k = (number - rest) / pair.$$anonymous$$ey;
             float f = $$anonymous$$athf.Round(rest / (pair.$$anonymous$$ey / 10));
             string roundedNumber;
             if (f == 0)
             {
                 roundedNumber = k.ToString();
             }
             else
             {
                 if(f == 10)
                 {
                     f = 9;
                 }
                 roundedNumber = k.ToString() + "." + f.ToString();
             }

             return roundedNumber + pair.Value;
         }
     }
     return number.ToString();
 }
avatar image
0

Answer by camlam999 · Oct 05, 2016 at 01:40 PM

@ChrisAngelMindmapfreak I just made a quick new project to test this out, made a format script with your code in it and an "add" script which adds 50 to the 3d text as a score, i got no clue how to set this up or anything, half of it ive never seen, i try looking for videos that explain this but most my knowledge is from 1 of AwfulMedia's playlist :(

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mindmapfreak · Oct 05, 2016 at 02:19 PM 0
Share

How does your code look at the moment? You can paste the code of the AbbrevationUtility-class into any file and use it with AbbrevationUtility.AbbreviateNumber(YOURNU$$anonymous$$BERHERE).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

250 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to round to 2 decimals with format? (123456 > 123K > 123.45K) (C#) 0 Answers

Physics2d.Raycast always returning 0,0 0 Answers

Highscore not showing up 1 Answer

What is the difference between Getkey and Getkeydown (C#) 2 Answers

Recoil Problem 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges