- Home /
Question by
ManuelPaurevic · Jul 06, 2017 at 01:48 AM ·
c#uimobiletimer countdown
Rounding Numbers not consistent??? (C#)
So I made a timer that counts down 10 seconds and has 3 decimal places. The time that is getting taken away is making it have more then 3 decimals and I tried using "Mathf" but it would only work some times. I'll add my code and a video on whats the timer doing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class timer : MonoBehaviour {
private Text textBox;
public float timeTaken;
// Use this for initialization
void Start () {
textBox = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
timeTaken -= Time.deltaTime;
timeTaken = Mathf.Round(timeTaken * 1000.0f) * 0.001f;
textBox.text = "" + timeTaken;
}
}
Comment
Please format your code properly, as a courtesy to readers.
Best Answer
Answer by Kishotta · Jul 06, 2017 at 02:02 AM
You can use ToString()
to specify formatting. No need to mess with multiplication or division.
textBox.text = timeTaken.ToString ("0.000");
Thank you didn't know it was that easy. I was just making it harder than it was.
Your answer
Follow this Question
Related Questions
CrossPlatformMobileInput Joystick.cs not working 0 Answers
Game Over Text When Timer Ends 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers