Create a float with only 1 decimal? C#
Hi! I'm trying to make a timer count upwards in my 2D mobile game, I got it to work but how can I round of a float to only 1 decimal? For instance the game now writes out "1.123". How can I change that to be "1.1", 1.2", 1.3" etc?
Code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TimerScript : MonoBehaviour
{
//Create timer
public static float timer = 0.0f;
//Create displayed timer
Text text;
void Awake()
{
//Load Text class
text = GetComponent<Text> ();
//Reset the timer
timer = 0.0f;
}
void Update()
{
timer += Time.deltaTime;
text.text = "" + timer;
}
}
Just search for "C# show decimal." Lots if general answers.
Another way to do it is use an int*10, and divide by 10 when you print.
Answer by Internetman · Mar 14, 2016 at 04:05 PM
Got it to work :) Just changed:
text.text = "" + timer;
to:
text.text = "" + timer.ToString("F1");
Your answer
Follow this Question
Related Questions
Timer float not evaluating correctly when I check if it's less than another number 3 Answers
Int not getting rounded data 0 Answers
Timer instantly sets to zero 1 Answer
Need help with making disappearing platforms. Error CS1525 1 Answer
I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer