Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by BenHUK, May 19, 2016.

  1. BenHUK

    BenHUK

    Joined:
    Mar 22, 2015
    Posts:
    5
    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?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UpgradeManager : MonoBehaviour {
    5.  
    6.     public Click click;
    7.     public UnityEngine.UI.Text itemInfo;
    8.     public float cost;
    9.     public int count = 0;
    10.     public int clickPower;
    11.     public string itemName;
    12.     private float baseCost;
    13.  
    14.     void Start(){
    15.         baseCost = cost;
    16.     }
    17.  
    18.     void Update() {
    19.         itemInfo.text = itemName + "\ncost: " + cost + "\nPower: +" + clickPower;
    20.     }
    21.  
    22.     public void PurchasedUpgrade() {
    23.         if (click.gold >= cost) {
    24.             click.gold -= cost;
    25.             count += 1;
    26.             click.goldperclick += clickPower;
    27.             cost = Mathf.Round (baseCost * Mathf.Pow (1.35f, count));
    28.         }
    29.     }
    30.  
    31. }
    Any help would be appreciated :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. BenHUK

    BenHUK

    Joined:
    Mar 22, 2015
    Posts:
    5
    Unfortunately I've tried adding those bits of code, and tried to add it in but I still can't get it to work :(
     
  4. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    How is it not working? Is it still showing as 1.2555e+07 or something similar?
     
  5. BenHUK

    BenHUK

    Joined:
    Mar 22, 2015
    Posts:
    5
    Yeah, it just shows as that, unfortunately I haven't found a fix for it since I'm quite new to scripting
     
  6. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    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.
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  8. BenHUK

    BenHUK

    Joined:
    Mar 22, 2015
    Posts:
    5
    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.
     
  9. BenHUK

    BenHUK

    Joined:
    Mar 22, 2015
    Posts:
    5
    I've added the code in from the answer thread, it looks completely nooby cause I don't know what to edit haha.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UpgradeManager : MonoBehaviour {
    5.  
    6.     public Click click;
    7.     public UnityEngine.UI.Text itemInfo;
    8.     public float cost;
    9.     public int count = 0;
    10.     public int clickPower;
    11.     public string itemName;
    12.     private float baseCost;
    13.  
    14.     void Start(){
    15.         baseCost = cost;
    16.     }
    17.  
    18.     void Update() {
    19.         itemInfo.text = itemName + "\ncost: " + cost + "\nPower: +" + clickPower;
    20.     }
    21.  
    22.     public void PurchasedUpgrade() {
    23.         if (click.gold >= cost) {
    24.             click.gold -= cost;
    25.             count += 1;
    26.             click.goldperclick += clickPower;
    27.             cost = Mathf.Round (baseCost * Mathf.Pow (1.35f, count));
    28.         }
    29.     }
    30.  
    31.     public static string itemNameUnit( double num )
    32.     {
    33.         double numStr;
    34.         string suffix;
    35.         if( num < 1000d )
    36.         {
    37.             numStr = num;
    38.             suffix = "";
    39.         }
    40.         else if( num < 1000000d )
    41.         {
    42.             numStr = num/1000d;
    43.             suffix = "K";
    44.         }
    45.         else if( num < 1000000000d )
    46.         {
    47.             numStr = num/1000000d;
    48.             suffix = "M";
    49.         }
    50.         else
    51.         {
    52.             numStr = num/1000000000d;
    53.             suffix = "B";
    54.         }
    55.         return numStr.ToString() + suffix;
    56.     }
    57.  
    58. }
    59.  
     
  10. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    The issue I see is that you're not calling the new code, so it's not doing anything but taking up space.