0
\$\begingroup\$

Apologies for sloppy work and probably a poor grasp of fundamentals.

I have a (likely primitive) way to convert the ever increasing number values of my enemy hp into a string format to display in game. (K for thousands, M for millions, B for billions etc)

 if (target_healthmax >= 1000)
     {
         if (target_healthmax >= 1000000000)
         {
             enemy_hp_text.text = "Health: " + (target_healthcur * 0.000000001f).ToString("#.00") + "/" + (target_healthmax * 0.000000001f).ToString("#.00") + "B";
         }
         else
         {

             if (target_healthmax >= 1000000)
             {
                 enemy_hp_text.text = "Health: " + (target_healthcur * 0.000001f).ToString("#.00") + "/" + (target_healthmax * 0.000001f).ToString("#.00") + "M";
             }
             else
             {
                 enemy_hp_text.text = "Health: " + (target_healthcur * 0.001f).ToString("#.00") + "/" + (target_healthmax * 0.001f).ToString("#.00") + "K";
             }
         }
     }
     else
     {
         enemy_hp_text.text = "Health: " + target_healthcur + "/" + target_healthmax;
     }

but I stopped here after I noticed that although I could convert long numbers I would eventually hit a number too large to handle such as "-2.147484e+09" in my editor, and it would stop working.

I am using floats currently, is there a different type of variable that handles this easily? if not, what is an efficient way of solving this?

Would it be efficient to convert every new factor of 100 to a new variable and only handle the highest valued variable? eg

if num >= 1000, k += 1, num = 0;

 if k>= 1000, m += 1, k = 0;
 ...

(Please feel free to let me know anything about my post I may need to amend, I am new here)

\$\endgroup\$
3
  • \$\begingroup\$ How much do you care about precision for higher values? \$\endgroup\$
    – Kyy13
    Mar 2, 2018 at 20:37
  • \$\begingroup\$ Currently, I don't have anything implemented that requires me to care about exact precision for higher values, if the values still behave in a way I can predict in terms of growth/display etc. \$\endgroup\$
    – bumble
    Mar 2, 2018 at 21:05
  • \$\begingroup\$ Hello and welcome to gamedev.se! As noticed, this question has been asked before. If it does not solve your issue, please edit the question and highlight the differences and why the other question does not answer yours. We may be able to re-open it. \$\endgroup\$
    – Vaillancourt
    Mar 3, 2018 at 3:07

2 Answers 2

0
\$\begingroup\$

-2.147484e+09

That looks like the maximum of a 32-bit signed integer. are you sure target_healthmax is a float?

A 32-bit float should give you something like a 38-digit value, and a 64-bit double will give you a 308-digit value.

\$\endgroup\$
1
  • \$\begingroup\$ Oops, you're right. I'm using a float but using Mathf.RoundToInt on them somewhere to keep them as whole numbers, I'll revisit that section. \$\endgroup\$
    – bumble
    Mar 2, 2018 at 21:02
0
\$\begingroup\$

If you need really big numbers, you need a BigNumber library.

I've been using this one myself, although there may be some bugs in it. I've attempted to open issues and get fixes in, but the author doesn't look to be around any more.

They're easy things to fix (check the issues tab, the first two are the ones I've reported and has enough information to apply the fixes), though it may be worth looking for a different library instead. I founds that one when I was looking for one that was already in C# and would work on Unity's .NET version (if you use Unity 2017 or newer, you should be able to use Microsoft's own System.Numerics library).

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .