How to round a float to 2 decimal places?

Hey guys,
So what I want to do is round my “score” float number to 2 decimal places, so it looks neater :slight_smile:

My code so far:
```csharp
**using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;

public class ScoreManager : MonoBehaviour
{
public static float currentweight = Mathf.Round(currentweight * 10) / 10; // The player’s current weight.
Text text; // Reference to the Text component.

void Awake ()
{
    // Set up the reference.
    text = GetComponent <Text> ();

    // Reset the score.
    currentweight = 0;
}

void Update ()
{
    // Set the displayed text to be the word "Weight" followed by the currentweight value.
    //Math.Round(currentweight, 2);
    text.text = "Weight: " + currentweight + " kg";
}

}**
```

As you can see what I’ve tried so far is:

Math.Round(currentweight, 2);

and…

    public static float currentweight = Mathf.Round(currentweight * 10) / 10;

Neither of these work…

This is what my output looks like at the moment:

Help is appreciated! :smile:

Your main problem is that all the math you’re doing there would only be done at initialization; you need it to be done every frame. in Update, before you update the text, THAT’s where you would want to put rounding code.

However, it’ll be easier/better to simply limit the display, rather than rounding the actual number. (For example, if someone adds 0.004 kg three times, you want it to display an increase; rounding the actual number would remove that.) Many classes, including float, have a ToString variant that accepts a formatting string to tell it how to display a number:

currentweight.ToString("#.00");

Also, there are performance reasons to avoid using concatenation (that is, +) when building strings. Use string.Format instead:

text.text = string.Format("Weight: {0:#.00} kg", currentweight);

For a bit of elaboration: you can put as many variables after the string as you like, and anything in {} will refer to those by index. {0} will be replaced with the first variable, {1} with the second, etc. And you can put : in there, and anything after the : will be a formatting string for that variable (just the same as the parameter sent to .ToString above). There’s a ton of string.Format and ToString documentation out there (MSDN’s page is pretty thorough), this is just the basics.

3 Likes

Awesome thanks StarManta!
How would I make the string be equal to “Weight: 0 kg” instead of “Weight: 0.0kg” at game start?
Just for pure cosmetic reasons? :smile:

Uh… hm, I guess you’d have to make a special case for that? Just “if weight = 0 this, else that”.

Ah awesome, I just did that, working perfectly.
Thanks for the help! :slight_smile:

For future reference,
The Solution:
```csharp
**using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;

public class ScoreManager : MonoBehaviour
{
public static float currentweight; // The player’s current weight.
Text text; // Reference to the Text component.

void Awake ()
{
    // Set up the reference.
    text = GetComponent <Text> ();

    // Reset the score.
    currentweight = 0;
}

void Update ()
{
    // Set the displayed text to be the word "Weight" followed by the currentweight value.
    if (currentweight == 0)
    {
        text.text = string.Format("Weight: 0 kg");
    }
    else
    {
        text.text = string.Format("Weight: {0:#0.0} kg", currentweight);
    }
}

}**
```

1 Like

Since you’re not inserting a variable value into the string, the string.Format on line 25 is not necessary.

Correct me if I am wrong but don’t ToString method just hides the digit do not round it off?

Ok I’ll change line 25. :smile:
And yep this is cutting/hiding all the digits after the number after the first decimal point.
Maybe the title is a bit misleading now… :smile: