Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mikenekro · Aug 10, 2014 at 03:28 AM · displaynumberscash

GUI Display in game cash help

Ok so I can get the users money to be displayed in the screen, change the text when the user reaches a certain amount of money but where I am having trouble is converting the amount for the respective symbols like Thousands (K) Millions (M) Billions (B). Since the players will be working with a high amount of money throughout the game, I believe this method would work the best for reading how much money you have and since I cannot figure out how to display 10,000,000 without unity having it say 1E+07.

Lets say the player has $900M ($900 Million) then they gain $100M and now they have $1000M or $1B ($1 Billion). How would I go about displaying the number 1000 as 1 or 1789 as 1.789 but have it still acting like 1000 or 1789?

I'll put my test code in here for a reference as to what I have so far. It works but I just can't figure out the conversion part of it so I have nothing for that in there.

 C# Code
 using UnityEngine;
 using System.Collections;
 
 public class UpgradeWindow : MonoBehaviour {
 
     public GUIStyle myStyle;
 
     void OnGUI ()
     {
         if(GameControl.control.cash1 < 1000f)
         {
 
             GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cash1 + " M", myStyle);
 
         } 
         else if(GameControl.control.cash1 >= 1000f)
         {
 
             GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cash1 + " B", myStyle);
 
         }
 
         if(GUI.Button(new Rect(100, 200, 200, 60), "Upgrade", myStyle))
         {
 
             GameControl.control.cash1 += 100.07f;
 
         }
 
         if(GUI.Button(new Rect(100, 300, 200, 60), "Degrade", myStyle))
         {
 
             GameControl.control.cash1 -= 100.06f;
 
         }
 
     }
 }


EDIT

I ended up figuring it out but to me the code looks a bit messy.

I changed cash1 to 3 different variables and changed them to decimals instead of floats like this.

 C# Code
 GameControl Script
 
 public decimal cashK1;
 public decimal cashM1;
 public decimal cashB1;


Then I made these changes to my code.

 using UnityEngine;
 using System.Collections;
 
 public class UpgradeWindow : MonoBehaviour {
 
     public GUIStyle myStyle;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(GameControl.control.cashK1 < 0.00M)
         {
             GameControl.control.cashK1 = 0.00M;
             GameControl.control.cashM1 = 0.00M;
             GameControl.control.cashB1 = 0.00M;
              //the above code makes sure you cannot get a negative value
         }
     }
 
     void OnGUI ()
     {
         if(GameControl.control.cashK1 < 1000.00M)
         {
 
             GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cashK1 + " K", myStyle);
 
         } 
         if(GameControl.control.cashK1 >= 1000.00M && GameControl.control.cashK1 < 1000000.00M)
         {
 
             GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cashM1 + " M", myStyle);
 
         }
         if(GameControl.control.cashK1 >= 1000000.00M)
         {
             
             GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cashB1 + " B", myStyle);
 
         } 
 
         if(GUI.Button(new Rect(100, 200, 200, 60), "Upgrade", myStyle))
         {
                 //Added all three decimal variables to always keep them equivalent to each other
                 GameControl.control.cashK1 += 50000.00M;
 
                 GameControl.control.cashM1 += 50.00M;
                 GameControl.control.cashB1 += 0.05M;
 
         }
 
         if(GUI.Button(new Rect(100, 300, 200, 60), "Degrade", myStyle))
         {
 
                 GameControl.control.cashK1 -= 50000.00M;
                 
                 GameControl.control.cashM1 -= 50.00M;
                 GameControl.control.cashB1 -= 0.05M;
 
         }
 
     }
 }
 

If someone sees that I am using too much code and know how to shorten it, I would appreciate it.

Also sorry for asking a question then figuring out how to solve it before anyone can answer. It seems like the only time I can figure something I have been stuck on for a few days is to ask the question.

Maybe typing out my question helps me actually see what I am looking for?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tanoshimi · Aug 10, 2014 at 07:09 PM

Don't store your amount of cash in three different variables - that's just horrible! To display the amount of cash with the appropriate suffix, just create a new function like this:

 static string FormatCash(long num)
 {
 if (num >= 100000000) {
     return (num / 1000000D).ToString("0.#M");
 }
 if (num >= 1000000) {
     return (num / 1000000D).ToString("0.##M");
 }
 if (num >= 100000) {
     return (num / 1000D).ToString("0.#k");
 }
 if (num >= 10000) {
     return (num / 1000D).ToString("0.##k");
 }

 return num.ToString("#,0");
 }

(Note I've put your cash as a long - I doubt you want a decimal, and certainly not a float). Then, in all your OnGUI code, just call FormatCash(amountOfCash). This will return the following:

 123        ->  123
 1234       ->  1,234
 12345      ->  12.35k
 123456     ->  123.4k
 1234567    ->  1.23M
 12345678   ->  12.35M
 123456789  ->  123.5M
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikenekro · Aug 11, 2014 at 08:16 PM 0
Share

Awesome Thanks. I haven't been coding for very long so the only thing I am having trouble with is adding/subtracting an amount from the gui. I can get it to show the amount with the appropriate suffix but when I try to add or subtract from that gui nothing happens.

I'm sure this probably is not the correct way to do this but this is what I am doing.

 C# Code
 
 GUI.Label (new Rect (70, 65, 100, 30), "Cash: $ " + FormatCash(0), myStyle);

 if(GUI.Button(new Rect(100, 200, 200, 60), "Upgrade", myStyle))
         {
 
             FormatCash(10000);
 
         }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Changing Health Display GUI 1 Answer

How to reduce number of digits in coordinate points? 2 Answers

Text Mesh Pro - Unicodes 0 Answers

Animation not showing when close up 1 Answer

Bug when hiding resolution Dialog? (Settings get reset on startup) 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges