- Home /

# how to round a float to 2 DP

i have found functions that truncate the decimal places to create an integer.

but i have the time in seconds and wish to have a set amount of decimal places say 3.

thanks

edit: in C# please

**Answer** by Mike 3
·
Mar 03, 2011 at 04:04 PM

If it's just for changing to string:

```
yourFloat.ToString("F2")
```

If you want the float itself truncated:

```
yourFloat = Mathf.Round(yourFloat * 100f) / 100f;
```

**Answer** by yoyo
·
Mar 03, 2011 at 04:05 PM

To get time in thousandths of a second, you could do this (C# syntax) ...

```
float seconds = Time.time;
int thousandths = (int)(seconds * 1000.0f);
```

The C# system library includes Math.Round, which lets you pass in the number of decimal places you want to preserve. However it works with double and decimal numbers, which may not be desirable. Unity's Mathf.Round does not let you specify decimal places (feature request??), but you can implement your own version like this:

```
public static float Round(float value, int digits)
{
float mult = Mathf.pow(10.0f, (float)digits);
return Mathf.Round(value * mult) / mult;
}
```

Note that it will be more efficient to just perform the computation inline with your code, knowing the appropriate power of 10, for example:

```
float rounded = Mathf.Round(seconds * 1000.0f) / 1000.0f;
```

or (if you don't care about rounding):

```
float rounded = (int)(seconds * 1000.0f) / 1000.0f;
```

Finally, if this is just for formatting and display, not calculation, then there are several alternatives shown in this question.

It is Mathf.Pow not Mathf.pow, just in case you get an error.

**Answer** by estefanowolf
·
Nov 15, 2014 at 09:24 AM

```
//create a double
//create a float
float a;
double b;
b = System.Math.Round(a,2);
// replace number 2 with number of decimals you wish to round upto.
```

By far the simplest solution, System.$$anonymous$$ath.Round(a,2);

Question, will this allow the file size to be smaller, or is it the same as "using System;" namespace? In other words, will this only extract the math.round method from system without importing all the other methods?

**Answer** by charmandermon
·
Aug 07, 2013 at 05:07 AM

just add this at the top:

```
using System;
```

It will give you access to math.round

Then do something cool like this:

```
speedCapped = (float)Math.Round((double)lerpedSpeed,2);
```

Its a little over the top but cool.

**Answer** by masterarcher
·
Mar 03, 2011 at 04:06 PM

i foudn this example in the end on stackoverflow.:

decimal a = 1.994444M;

Math.Round(a, 2); //returns 1.99

decimal b = 1.995555M;

Math.Round(b, 2); //returns 2.00

i think this is a little neater than yours Mike, cheers for the reply tho.

bah math isnt there and its not an overloaded function of mathf

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.