Converting Gold to a Unit (1000 gold = 1k)

Hey Guys,

I’m stuck in C#, I have a script for buttons which displays the cost, count and click power. But when the gold starts to reach over 10+ million the number becomes something like 1.2555e+07. I’m wanting to simple it down, so 1000 gold = 1k, 1000000 = 1 Mil. How would I go about doing this?

using UnityEngine;
using System.Collections;

public class UpgradeManager : MonoBehaviour {

    public Click click;
    public UnityEngine.UI.Text itemInfo;
    public float cost;
    public int count = 0;
    public int clickPower;
    public string itemName; 
    private float baseCost;

    void Start(){
        baseCost = cost;
    }

    void Update() {
        itemInfo.text = itemName + "\ncost: " + cost + "\nPower: +" + clickPower;
    }

    public void PurchasedUpgrade() {
        if (click.gold >= cost) {
            click.gold -= cost;
            count += 1;
            click.goldperclick += clickPower;
            cost = Mathf.Round (baseCost * Mathf.Pow (1.35f, count));
        }
    }

}

Any help would be appreciated :slight_smile:

http://stackoverflow.com/questions/30992304/formatting-large-numbers-in-c-sharp

Unfortunately I’ve tried adding those bits of code, and tried to add it in but I still can’t get it to work :frowning:

How is it not working? Is it still showing as 1.2555e+07 or something similar?

Yeah, it just shows as that, unfortunately I haven’t found a fix for it since I’m quite new to scripting

Is it showing the 1.2555e+07 after you divide by 1000000 or whatever amount? Because the answer to the question on the link that LeftyRighty gave should work.

and until you show us how you tried to do it we really can’t tell you where you’ve gone wrong.

“it’s not working” isn’t sufficient information for us to help you.

I didn’t say that, I’m trying to explain it as best as I can, I’ve tried to add bits of that code in but I’m not sure where to add it or what to edit.

I’ve added the code in from the answer thread, it looks completely nooby cause I don’t know what to edit haha.

using UnityEngine;
using System.Collections;

public class UpgradeManager : MonoBehaviour {

    public Click click;
    public UnityEngine.UI.Text itemInfo;
    public float cost;
    public int count = 0;
    public int clickPower;
    public string itemName; 
    private float baseCost;

    void Start(){
        baseCost = cost;
    }

    void Update() {
        itemInfo.text = itemName + "\ncost: " + cost + "\nPower: +" + clickPower;
    }

    public void PurchasedUpgrade() {
        if (click.gold >= cost) {
            click.gold -= cost;
            count += 1;
            click.goldperclick += clickPower;
            cost = Mathf.Round (baseCost * Mathf.Pow (1.35f, count));
        }
    }

    public static string itemNameUnit( double num )
    {
        double numStr;
        string suffix;
        if( num < 1000d )
        {
            numStr = num;
            suffix = "";
        }
        else if( num < 1000000d )
        {
            numStr = num/1000d;
            suffix = "K";
        }
        else if( num < 1000000000d )
        {
            numStr = num/1000000d;
            suffix = "M";
        }
        else
        {
            numStr = num/1000000000d;
            suffix = "B";
        }
        return numStr.ToString() + suffix;
    }

}

The issue I see is that you’re not calling the new code, so it’s not doing anything but taking up space.