0

I am trying to make a game with untiy and c# which is about numbers.

However i need metric prefixes for huge numbers (kilo,mega,giga etc)

i did something like this till now:

normal += 9;
  if(normal >= 999)
  {
    normal = 0;
    kilo += 1;
  }
  if(kilo >= 999)
  {
    kilo = 0;
    mega += 1;
  }

but i faced a problem that when the numbers go 981 990 999 the normal number reset instead of being 1008

also how can i do calculation on prefixes?

edit#1:

i have to objects (in unity) when i click on object "ASDASD" i get money and when i click on Object "QQQ" i buy something

this code is for ASDASD

public float normal;
    public float kilo;
    public float mega;
    public float x;

    public GUIText Displayer;



    void Start () 
    {



    }

    void OnMouseDown ()
    {

        if (gameObject.name=="ASDASD")
        {
        normal =normal + 90000;

        if(normal > 99999)
        {

           x=normal-100000;

           normal = 0;

           kilo = kilo + 1 + x/100000f;


        }
        }
    }


    void Update ()

    {

        if (kilo==0)

           Displayer.text = "" + normal.ToString("n0");

        else if (kilo >=1)

            Displayer.text="" + (kilo*100+normal/1000).ToString("n0") + "K";




    }
}

now how can i do the second object code ? (buy something)

Rune Vejen Petersen
  • 3,201
  • 2
  • 30
  • 46
Forenkazan
  • 97
  • 2
  • 2
  • 8

2 Answers2

1

I think your choose a wrong way. You should have one variable for price and a function to convert it to text representation. Answer for that

Community
  • 1
  • 1
vsenik
  • 518
  • 5
  • 12
0

I have recently create this function :

string[] prefixeSI = {"y", "z", "a", "f","p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y"};
string numStr(double num)
{
    int log10 = (int)Math.Log10(Math.Abs(num));
    if(log10 < -27)
      return "0.000";
    if(log10 % -3 < 0 )
        log10 -= 3;
    int log1000 = Math.Max(-8, Math.Min(log10 / 3, 8));

    return ((double)num / Math.Pow(10, log1000 * 3)).ToString("###.###" + prefixeSI[log1000+8]);
}


Console.WriteLine(numStr(1000000)); // 1M
Console.WriteLine(numStr(100000));  // 100k
Console.WriteLine(numStr(10000));   // 10k
Console.WriteLine(numStr(1000));    // 1k
Console.WriteLine(numStr(100));     // 100
Console.WriteLine(numStr(10));      // 10
Console.WriteLine(numStr(1));       // 1
Console.WriteLine(numStr(0));       // 0.000
Console.WriteLine(numStr(0.1));     // 100m
Console.WriteLine(numStr(0.01));    // 10m
Console.WriteLine(numStr(0.001));   // 1m
Console.WriteLine(numStr(0.0001));  // 100µ

//Over Yotta
Console.WriteLine(numStr(4545689486541536356525425482.64786));// 4545.689Y
//Under yocto
Console.WriteLine(numStr(-0.000000000000000000000001));       // -1y
Console.WriteLine(numStr(-0.000000000000000000000000001));    // -.001y
Console.WriteLine(numStr(-0.0000000000000000000000000001));   // 0.000

This is look good, but you can see the limit when you have a number under 0.001y : it write nothing ! (But, I'm rarely using numbers like 10E-27 =P)

Of cours, you can delete "If(log10 < -27)", but it will just return "y" for 0 and numbers like 10E-27

nitrateag
  • 1
  • 3