- Home /

# Big Numbers - How to convert (k, M, a )

**Hello!**

I'm making an Idle Click Game and I have some problems about the way to handle big numbers.

First of all, I'm not sure yet which variable type I should use to save the player's money. My options are Long, BigInteger, Decimal.

The first problem is that I need to be able to Serialize the numbers and some of them can't be serializable (bigInteger). I would have to save it as a String and convert all the time.

Also What do I do when the money reaches the limit of the **variable** (Long, BigInteger or Decimal) ?

I need a way so when he gets to the limit, I start using maybe an array of this **variable** and the total money it's the sum.

**Can someone advice me about this question ?**

**Second question:**

How can I convert the money in a way so I could display :

1 k instead of 1 000

1 M instead of 1 000 000

1 B instead of 1 000 000 000

1 T instead of 1 000 000 000 000

1 a instead of 1 000 000 000 000 000

1 b instead of 1 000 000 000 000 000

etc

I'm really lost in this problem and I need to solve it.

I would love if someone helps me.

**Thank you** in advance !

**Answer** by Eno-Khaon
·
Sep 14, 2018 at 03:55 AM

If you're especially worried about perfect accuracy, you may want to dig around the internet a bit to find a solution that's suitable for your needs. The first few examples I found as far as related links go include **here** and **here**.

If 100% perfect accuracy isn't important (and, looking at this realistically, it might not be when you consider that 150,000 out of 500 duoquinquagintillion is genuinely insignificant), you could try taking a different approach to this.

For example, you might consider comprising your values of a pair of numbers. The first would be the general value, where the second is the exponent of 10 to apply to it. If you had a base value of 3.424126 and an exponent of 10, the resulting number would be 34,241,260,000. It's fundamentally like what floating point numbers do internally, but you'd just assemble the number yourself (seeing how the first number would even be a float in the first place).

To compare values, you'd multiply the difference in exponents with the base value. For example,

3.4^{e10} - 2.2^{e9} = 3.18^{e10}

3.4 - (2.2 * 10^{9-10}) = 3.18

3.4 - (2.2 * 10^{-1}) =3.18

3.4 - 0.22 = 3.18

If the difference between values is greater than a number of significant digits specified (for instance, Unity's floating point has 7 digits of accuracy), then the difference can potentially be considered large enough to ignore the math altogether.

3.4^{e10} - 5^{e2}

2 - 10 = -8

|-8| > 7 (accuracy)

As far as it matters...

3.4^{e10} - 5^{e2} = 3.4^{e10}

If you put together a general value-type script to utilize number pairs in that manner, you can also make quick assumptions for shortcuts. For instance, if the exponent for a "purchase" is greater than your current "money" exponent, you already know you can't afford it.

Edit: For additional reading, **consider this list of large number naming conventions**.

Thank you so much for your detailed answer !

Basically I save 2 values (float and int) and use them to calculate the values of stuff, right ?

Using this method I think it's pretty easy to write the number in the screen.

If the money is 3.4e6 , I just need to check the exponential (6) . If it's 6, then the player has 3.4 $$anonymous$$illion Coins.

You are awesome. Thank you so much.

Now I "just" have to write the functions :)

Happy to help. Yeah, it'll involve a bit of script-writing to make it all fit together nicely, but it shouldn't be **too** bad.

The key point to keep in mind with this approach will be that comparing exponents will be the ever-important first step. You can't rely on converting the values first, since the true, complete values (even as differences between them) may easily exceed the accuracy available to the float values themselves.

Furthermore, you can also give yourself string output variations in the script for when you want to display it shorthand (1e15) or verbose (1 quadrillion). If you wish to pursue that route, then a little research on the naming patterns should let you assemble value names based on the exponent value (or, rather, its multiple of 3).

Thanks man :D

I'm implementing the functions right now. How can I keep a float with only 3 decimal places.

1,024 ins$$anonymous$$d of 1,024523

I know how to write in the console with just 3 decimal places, but I want to save the value as 1,024. Or maybe I can write a function where : if (decimalPlaces == 4) ignore(lastOne)

How do I accomplish this?

Edit: Or maybe I'm overthinking and I don't need to worry about it. Just need to show the value with 3 decimal places.

Thank you very much

Sorry to burst your bubble on this one, but your suggested solution is pretty pointless ^^. A normal float already has a range of 10^38 which is already huge. Extending just the exponent manually just increases the overall range but you stay at the same precision which is pretty bad the higher the values get. See this table. Even you can represent such huge numbers you can't really do any useful math with them since the usable range is relative to the binary point. If the number is about 100 million the smallest change you can apply to the number is about "4". This relative distance scales linearly. So when you have 100 billion the smallest representable change would be around "4000".

Just using double ins$$anonymous$$d of float will boost the actual significant digits but also the exponent range. double has a range of about 10^300.

If all the number scale linearly with your max amount it would be just a matter of using different suffix letters while using the same down scaled number. If you actually need to work with those numbers it would be more practical to split the number into a float and a "long" part. Say you allow the float a range of +-1000 to keep the precision. If the float gets larger than 1000 you would carry those 1000 as 1 into the long value. The long type goes up to 9,223,372,036,854,775,807. Since value each represents 1000 you get an even larger range.

Of course it's not as large as any floating point solution but it can at least work with small changes like "+1". Again the single precision float looses the "+1" ability around 32 million. If you want to be able to *smoothly* increase a value by 1 every second you would need the ability to add 1/60 every frame (when running at a framerate of 60fps). So that would be "0.01666". Using just a float (with or without extended exponent) you can't go higher than about 100k in total

Due to the general nature of the "Idle Click Game" genre, much of the point of them is purely to reach ridiculously huge numbers for that sense of endless "progress" throughout them.

as an example of how over-the-top the number systems in this genre can be, **Clicker Heroes** reaches money values exceeding 1.0*10^{50000}. Despite that, it starts off with single-digit numbers.

It's definitely true that smaller values (relatively speaking) will become insignificant after just a few factors of ten. With that in mind, along with consideration for the extraordinary numbers these games intentionally reach toward, my aim is to provide a means of reaching excessively large numbers, providing feedback on values while they're still meaningful (within a few factors of ten), and also trying to keep the math library simpler and cheaper by not relying on a 128-bit+ math library.

While your points are all perfectly valid (and normal, really), this genre of game has a knack for ignoring all typical, sensible logic when it comes to numbers. Once you reach those high values, nothing will visibly matter for millennia or more unless they're "close enough" already.

Wow!

This is to theoretical for me ahah! I think I understood just an half of it

### Your answer

### 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

### Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to transfer time script to 24 hours and not decimals 1 Answer

why don't decimals work? 2 Answers