- Home /
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!
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);
}
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?
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
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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