- Home /
The question is answered, right answer was accepted
How do I get rid of the numbers after the decimal?
I have this piece of code that does a countdown and it works fine, but it shows the numbers 5 places after the decimal does anyone know how I can get rid of this? I wouldnt even mind it just showing the numbers 2 places after the decimal. More preferably I would like the numbers after the decimal gone though. here is my code:
var timeLv1 : GUIText;
var timer : float = 30.000000000;
var outOfTime : boolean = false;
function level1 () {
timer -= Time.deltaTime;
timeLv1.text= "time: " + timer;
if(timer <= 0){
timer = 0;
outOfTime = true;
}
}
function Update () {
if(Application.loadedLevelName == "gameLvl1"){
level1();
if(outOfTime){
GameOver();
}
}
Answer by Eric5h5 · Mar 08, 2014 at 04:08 AM
timeLv1.text= "time: " + timer.ToString("f0");
Answer by Pecek · Mar 08, 2014 at 03:12 AM
Maybe there is a builtin function for this, but it would probably do exactly as this one: http://answers.unity3d.com/questions/197196/round-to-decimals.html
This does not seem to work for. It keeps saying $$anonymous$$athf is not apart of float
I just tested it and works fine here, can you please post your snippet?
@Pecek Here is What I have. I think I might just be inputing the code wrong. Can I see what you put?
var timeLv1 : GUIText;
var timer : float = 30.000000000;
var outOfTime : boolean = false;
function level1 () {
timer -= Time.deltaTime.$$anonymous$$athf.Round(randomNumber*100.0) /100.0;
timeLv1.text= "time: " + timer;
if(timer <= 0){
timer = 0;
outOfTime = true;
}
}
timer -= Time.deltaTime.$$anonymous$$athf.Round(randomNumber*100.0) /100.0;
this line can't be right, try using this ins$$anonymous$$d
timer -= $$anonymous$$athf.Round(Time.deltaTime *100.0) /100.0;
Also you can use Eric5h5's solution, but if you want to use it again as a number you'll have to convert it back(which looks a bit overkill for me)
Follow this Question
Related Questions
Unity/C# ignores decimal after it reach some value 1 Answer
Does parseFloat depend on the OS? 2 Answers
comma on PC - dot on Mac 1 Answer
Playerpref.SetInt enterd with a float. 2 Answers