- Home /
Getting 2 decimals instead of 1 with f1.
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 currencyPerSec, bool currencyPerClick)
{
string converted;
if(valueToConvert >= 1000000)
{
converted = (valueToConvert / 1000000f).ToString("f1") + " M";
}else if(valueToConvert >= 1000)
{
converted = (valueToConvert / 1000f).ToString ("f1") + " K";
}else
{
converted = "" + valueToConvert;
}
if(currencyPerSec == true)
{
converted = converted + " sps";
}
if(currencyPerClick == true)
{
converted = converted + " spc";
}
return converted;
}
}
I am pretty new to c# so please give easy explanation if possible. Thanks.
I'm using this code for a per second tick. I want to get say "1.1" but instead im getting "1.10" and it sometimes get stuck with a 9 on the end that just stays there.
Comment
Where are you seeing it displayed that way? Debug.Log the value and see if it looks correct in the log. If it does then your convert function is fine and the problem is in the display somehow.
Answer by Jessespike · Jul 09, 2015 at 08:29 PM
Change "f1" to "0.0"
(valueToConvert / 1000f).ToString("0.0")
https://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.110%29.aspx#Specifier0
Your answer