Need Help with C# for adding suffix to numbers!

So I am creating my very first game (incremental), i have been for a while now but i am a beginner at Unity. I am trying to learn by trial and error and have overcome issues ive posted previously but I am now stuck again.

I have created a “GoldPerSecond” display and have it setup and working, however i now want to implement suffix’s to the numbers so instead of 1000 it will be 1k 1000000/1m and so forth. Now i figured this out previously for my menu and Gold display but with my current script I keep getting an error… I am using the same Suffix script for all my other scripts and guessing it wont work with my coding on this script.

using UnityEngine;
using System.Collections;

public class gps : MonoBehaviour {

    public UnityEngine.UI.Text gpsDisplay;
    public RocksClick click;
    public ItemManager[] items;
    public static string KMBMaker(double num)
    {
        double numStr;
        string suffix;
        if (num < 1000d)
        {
            numStr = num;
            suffix = "";
            return numStr.ToString("0.##");
        }
        else if (num < 1000000d)
        {
            numStr = num / 1000d;
            suffix = "K";
            return numStr.ToString("0.##") + suffix;
        }
        else
        {
            numStr = num / 1000000d;
            suffix = "M";
        }
        return numStr.ToString("F2") + suffix;
    }

    void Start (){
        StartCoroutine (AutoTick ());
 
    }

    void Update(){
        gpsDisplay.text = [COLOR=#ff0000]KMBMaker(GetGoldPerSec)[/COLOR] () + " g/sec";

    }

    public int GetGoldPerSec(){
        int tick = 0;
        foreach (ItemManager item in items) {
            tick += item.count * item.tickvalue;
        }
            return tick;
    }

    public void AutoGoldPerSec(){
        click.gold += GetGoldPerSec ();
    }

    IEnumerator AutoTick(){
        while (true) {
            AutoGoldPerSec ();
            yield return new WaitForSeconds (1);
        }
    }
}

I have highlighted my error codes with (colour code) in Void Update. I know it is too do with the “GetGoldPerSec” being an Int but not sure how to change this or change my suffix/formatting code…

The KMBMaker coding i found on here a while ago but has been the only one I figured how to use

Thanks in Advance for any help given <3

-GuzLe

I couldn’t test this since I don’t have the rest of your project but it seems the problem is in Update(). Change it to:

    void Update()
    {
        // New code
        gpsDisplay.color = Color.red;
        gpsDisplay.text = KMBMaker(GetGoldPerSec()) + " g/sec";
       
        // Old code, don't use this.
        //gpsDisplay.text = [COLOR =#ff0000]KMBMaker(GetGoldPerSec)[/COLOR] () + " g/sec";
    }

haha my bad dude I never meant that. I Highlighted the “KMBmaker(GetGoldPerSec)” and it came up with the color code. Thats not in my script. The error is in the KMBMaker part. I know why i have the error i just dont know how to change my Script too fix it

Thanks for the reply though dude!

What’s the actual error message?

Oh lol :P. I’ve tested the KMBMaker part on it’s own and it seems to be working fine. What error message did you get?

CS1503 - Argument 1: Cannot convert from “method group” to double
Line 39

I think its because the GetGoldPerSec is made as a public int? Maybe you guys another way of converting format to K,M,B,T which works with int?

I assume this is what that line of code looks like without the bbcode format tags?

gpsDisplay.text= KMBMaker(GetGoldPerSec)() + " g/sec";

Move the () inside the argument list for KMBMaker so it looks like this

gpsDisplay.text = KMBMaker(GetGoldPerSec()) + " g/sec";

Dude!!! All this time and all i had too do was that!
Thank you so much <333333333333333

You truly are a life saver. Ive quit this project twice now and felt like doing it again xD! Coding for beginners is soooo stressful… Appreciate that mate i really do you GENIUS!

This can be so very true, but it can also be a lot of fun to figure things out! :slight_smile:

This is one of the reasons why I (and others) always try to suggest learning the basics first, as they will always help you through what you’re doing.

1 Like