Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Eagle-Bound · Aug 25, 2016 at 05:40 PM · c#scripting problemnoob

Idle game currency converter

I'm making an idle clicker game, like cookie clicker, and in order to save space I have tried to add a system that makes a number like 100000 100 K, or 10000000 10 Mil. My code is:

using UnityEngine; using System.Collections;

 public class CurrencyConverter : MonoBehaviour {
     
     private static CurrencyConverter instance;
     public static CurrencyConverter Instance {
         get {
             return instance;
         }
     }
     void awake(){
         CreateInstance ();
     }
 
     void CreateInstance () {
         if (instance == null) {
             instance = this;
         }
     }
 
     public string GetCurrencyIntoString(float valueToConvert, bool currencyPerSecond, bool currencyPerClick){
 
         string converted;
 
         if (valueToConvert >= 1000000) {
             converted = (valueToConvert / 1000000f).ToString ("3f") + " Mil";
         } else if (valueToConvert >= 1000) {
             converted = (valueToConvert / 1000f).ToString ("3f") + " K";
         } else {
             converted = "" + valueToConvert;
         }
 
         if (currencyPerSecond == true) {
             converted = converted + " Memes/Second";
         }
         if (currencyPerClick == true) {
             converted = converted + " Memes/Click";
         }
         return converted;
 
     }
 }


Separate script

 void Update () {
 
         MPC.text = "Memes/Click: " + CurrencyConverter.Instance.GetCurrencyIntoString (memesPerClick, false, true);
 
         memeDisplay.text = "Memes: " + CurrencyConverter.Instance.GetCurrencyIntoString (memes, false, false);
 
         MPSTxt.text = "Memes/Second: " + CurrencyConverter.Instance.GetCurrencyIntoString (memesPerSecond, true, false);
 

My problem is that the total memes are not being displayed at all, only the 'Memes: ' part, and the 'Memes/Click: ' only displays 'Button'

If anyone knows what I've done and how to make it function properly I would really appreciate it.

Thanks!

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

2 Replies

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

Answer by Jessespike · Aug 25, 2016 at 07:03 PM

You don't get an error? Instance should be null, because awake never gets called. The first letter needs to be upper-case. "Awake"

ToString ("3f") will just output "3f". I'm not sure of any elegant ways to show only the first 3 numbers, I know it can be done with multiple if-statements at least:

     public string CurrencyToString(float valueToConvert)
     {
         if (valueToConvert >= 1000000f) // millions
         {
             valueToConvert = (valueToConvert / 1000000f);
             if (valueToConvert >= 100f) return valueToConvert.ToString("0") + "M";
             else if (valueToConvert >= 10f) return valueToConvert.ToString("0.0") + "M";
             else return valueToConvert.ToString("0.00") + "M";
         }
         else if (valueToConvert >= 1000) // thousands
         {
             valueToConvert = (valueToConvert / 1000f);
             if (valueToConvert >= 100f) return valueToConvert.ToString("0") + "K";
             else if (valueToConvert >= 10f) return valueToConvert.ToString("0.0") + "K";
             else return valueToConvert.ToString("0.00") + "K";
         }
     
         return valueToConvert.ToString();
     }


 void Update () {
     MPC.text = "Memes/Click: " + CurrencyConverter.Instance.CurrencyToString(memesPerClick);
     memeDisplay.text = "Memes: " + CurrencyConverter.Instance.CurrencyToString(memes);
     MPSTxt.text = "Memes/Sec: " + CurrencyConverter.Instance.CurrencyToString(memesPerSecond);
 }
  
Comment
Add comment · Show 3 · 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 Eagle-Bound · Aug 25, 2016 at 09:13 PM 0
Share

Thanks! This works great. I have just one question. How would I make it so no more than three decimal places are showing at a time?

avatar image juicyz Eagle-Bound · Aug 25, 2016 at 09:33 PM 0
Share

If you use Jesse's code, then you can do ...ToString("#.000") Not sure on your exact needed end string.

http://www.csharp-examples.net/string-format-double/

http://stackoverflow.com/questions/3814190/limiting-double-to-3-decimal-places

and of course straight from microsoft https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx

avatar image Eagle-Bound juicyz · Aug 27, 2016 at 01:29 AM 0
Share

Thanks for your answer, @juicyz. However, I tried what you said, but it hasn't worked. $$anonymous$$y script now reads:

 if (valueToConvert >= 1000000f) // millions
         {
             valueToConvert = (valueToConvert / 1000000f);
             if (valueToConvert >= 100f) return valueToConvert.ToString("#.000") + " $$anonymous$$";
             else if (valueToConvert >= 10f) return valueToConvert.ToString("#.000") + " $$anonymous$$";
             else return valueToConvert.ToString("#.000") + "$$anonymous$$";
         }
         else if (valueToConvert >= 1000) // thousands
         {
             valueToConvert = (valueToConvert / 1000f);
             if (valueToConvert >= 100f) return valueToConvert.ToString("#.000") + " $$anonymous$$";
             else if (valueToConvert >= 10f) return valueToConvert.ToString("#.000") + " $$anonymous$$";
             else return valueToConvert.ToString("#.000") + " $$anonymous$$";
         }

I am new to coding, so perhaps it is an error on my part, but if not, what is wrong and how would you suggest I fix it?

avatar image
1

Answer by Bunny83 · Aug 27, 2016 at 03:38 AM

For things like that it's usually better to define a static method. Using a singleton for this case makes no sense. And to use a MonoBehaviour is even stranger ^^.

To keep things as generic as possible you might want to use something like this:

 private static string[] suffix = new string[]{"","k","M","G","T","P","E"}; // kilo, mega, giga, terra, penta, exa
 public static string CurrencyToString(double valueToConvert)
 {
     int scale = 0;
     double v = valueToConvert;
     while(v >= 1000d)
     {
         v /= 1000d;
         scale++;
         if (scale >= suffix.Length)
             return valueToConvert.ToString("e2"); // overflow, can't display number, fallback to exponential
     }
     return v.ToString("0.###") + suffix[scale];
  }

Note that the prefix "kilo" is always written lower case "k" while everything above (mega, giga, ...) is always an upper case letter.

"0.###" will display any number of digits in front of the decimal point (due to our dividing no more than 3) and will display at most 3 digits behind the decimal point. Some examples:

     Value                representation
 -----------------------------------
        0.5472            "0.547"
       10.1               "10.1"
      999.0               "999"
     3450.0               "3.45k"
    12782.3257            "12.782k"
   345755.4789            "345.756k" // the last 5 is rounded to 6
  2123548.12              "2.124M"   // the last 3 is rounded to 4

Of course if you want at least 1 decimal digit and at most 3 you can use "0.0##"

Also note i changed the type to double so it can be used for any floating point number.

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to destroy and instantiate something without breaking this code 1 Answer

My Animation Loops 3/4 of the way through 3 Answers

my ladder script not working plz help me 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 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