- Home /
Truncate number to first decimal place?
Hello, I was wondering how to truncate a number to the tenths place because the number gets too long and I just want it to be shorter but not just a whole number. Also I do not want the numbers to be rounded up or down because I do not want it to affect anything during the gameplay.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GlobalOres : MonoBehaviour
{
public static float OreCount;
public GameObject OreDisplay;
public float InternalOre;
public float InternalOreDisplay;
void Update()
{
InternalOre = OreCount;
InternalOreDisplay = InternalOre;
OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay;
if (OreCount >= 1000 & OreCount <= 1000000)
{
InternalOreDisplay /= 1000;
OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "K";
}
if (OreCount >= 1000000 & OreCount < 1000000000)
{
InternalOreDisplay /= 1000000;
OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "M";
}
if (OreCount >= 1000000000 & OreCount < 10000000000000)
{
InternalOreDisplay /= 1000000000;
OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "B";
}
if (OreCount >= 10000000000000 & OreCount < 100000000000000000)
{
InternalOreDisplay /= 10000000000000;
OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "T";
}
}
}
I tried adding Truncate into the division but it wouldn't divide the numbers, so I scrapped it.
Answer by Bunny83 · Jan 31, 2019 at 03:26 AM
Well you could use one of the standard string formatting strings. In your case the most suitable would be "F1". Though note that it uses the usual rounding. So 1234.56
would become "1234.6"
and 1234.54
would become "1234.5"
.
If you want to truncate the number, just use Mathf.Floor(x*10)/10
where x is the number you want to truncate to one digit behind the decimal point. I would still recommend to use "F1" since dividing a number by 10 could result in some .99999
number which would be rounded up. I don't have an actual example but i mean something like this: If you have a number like 1234.4896
you multiply it by 10 to get something like 12344.896
. Then floor will truncate the fractional part. However due to floating point accuracy the number you may get might be something like 12343.999997 or 12344.000002. Using ToString("F1") will ensure the resulting string will be "1234.4" in each case.
Note that you have to be careful when using &
. This is the bitwise "and" operator and not the logical and operator. In almost all cases you want to use &&
inside if statements to combine several boolean statements. Also your code could be simplified / optimised a lot. First of all it's bad practise to store variable as class members when they are only used inside a method. Use local variables. This simplifies debugging and makes the code more clear. It also does not clutter the class namespace. Next is (apart from using && which would slightly increase performance) you want to use an "else if" chain in such cases since only one of those cases should be valid at a given time.
The whole process of determining the order of magnitude could be done with a simple loop and a static array of strings
private static string[] suffixes = new string[] {"", "K", "M", "B", "T", "Q"};
private Text oreText;
void Start()
{
oreText = OreDisplay.GetComponent<Text>();
}
void Update()
{
float val = OreCount;
int suffix = 0;
while (val >= 1000)
{
val /= 1000;
suffix++;
}
if (suffix < suffixes.Length)
{
val = Mathf.Floor(val*10f) / 10f;
oreText.text = "Ores: " + val.ToString("F1") + suffixes[suffix];
}
else
{
// Too large, just use scientific format (i.e. 1.25E+010)
oreText.text = "Ores: "+ OreCount.ToString("E2");
}
}
This was very helpful. Also, thank you for the extra tips!
Your answer
Follow this Question
Related Questions
Help with strings and decimals 1 Answer
Simple math always returns 0 1 Answer
Why does this alway return 0 1 Answer
Simple Maths Problem - Help! 1 Answer
Math exponential number conversion 1 Answer