1

I'm still learning C# and I'm making simple clicker game. My problem is that if some numbers reach 10 000 000 they start to display as 1E+07 (for example). The point is that I would like it to be shown as 10mln and higher(bln etc). And every time I try to do it I got a lot of errors. Can you give me some advices how can I do it?

using UnityEngine;
using System.Collections;
using System;

public class ping : MonoBehaviour {

public UnityEngine.UI.Text gpc;
public UnityEngine.UI.Text GoldDisplay;
public static float gold = 0.00f;

public static long toInt64()
{

    return Convert.ToInt64(gold);
}
public int goldperclick = 1; 


void Update(){
    GoldDisplay.text = "Ping: " + gold;
    gpc.text = goldperclick + "ping/click";
}

public void Clicked(){
    gold += goldperclick;
}

}
Bonjean
  • 43
  • 6

2 Answers2

2

Probably you should implement your own type like Gold and override ToString() method for it

public class Program
{
    public static void Main()
    {
        Console.WriteLine(new Gold(10000));         //10000
        Console.WriteLine(new Gold(100000));        //100000
        Console.WriteLine(new Gold(1000000));       //1 mln.
        Console.WriteLine(new Gold(10000000));      //10 mln.
        Console.WriteLine(new Gold(100000000));     //100 mln.
        Console.WriteLine(new Gold(1000000000));    //1 bln.
        Console.WriteLine(new Gold(1500000000));    //1.5 bln.
    }
}

public class Gold
{
    public decimal Value {get;set;} 

    public Gold(decimal value)
    {
        Value = value;  
    }

    public override string ToString()
    {
        if(Value>=1000000000) return $"{Value/1000000000} bln.";
        if(Value>=1000000) return $"{Value/1000000} mln.";
        return Value.ToString();
    }
}

Also you should choose numeric type you use wisely regarding to their max values and precisions.

Here you can find some more details about large numbers formatting.

Community
  • 1
  • 1
mukh1n
  • 158
  • 1
  • 7
-1

If you're only ever dealing with whole numbers, you'd probably be better off using a long instead of a float.

A signed long or (int64) can hold a value up to 9,223,372,036,854,775,807 and will show every one of those values, while a float only provides 7 digits of precision - so with a value of 1,234,567,901 your float will only display 1,234,568,000 - which may not be what you expect (this can also be seen by printing out float.MaxValue.ToString("0") which only shows seven significant figures before filling with zeros).

An alternative if you need to use decimals would be to use a double which allows 15-16 digits of precision.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • I wanted to do it like that, but when I'm changing gold form flaot to long i got error: Cannot implicitly convert type 'float' to 'long'. So probably I'm doing it wrong. – Bonjean Mar 20 '17 at 15:30
  • @Bajor You'll need to update any references to `ping.gold` to also be `long`s as well. – Zhaph - Ben Duguid Mar 20 '17 at 18:13
  • Still got this error here: public class ItemManager : MonoBehaviour { public UnityEngine.UI.Text itemInfo; public ping ping; public long cost; public int tickValue; public long count; public string itemName; private long baseCost; void Start(){ baseCost = cost;} void Update(){ itemInfo.text = itemName + "\nKoszt: " + cost + "\nPing: " + tickValue +"/s";} public void PurchasedItem(){ if (ping.gold >= cost) { ping.gold -= cost; count += 1; cost = Mathf.Round (baseCost * Mathf.Pow (1.15f, count)); //<-error}} – Bonjean Mar 20 '17 at 21:37
  • `Mathf.Round` still returns a `float` not a `long` so you'll still get that error there. – Zhaph - Ben Duguid Mar 20 '17 at 21:48