- Home /

# Using Mathf.round for a vector3?

hello, I would like to know how to round to the nearest.5 for all vectors. meaning....

vector3(1.263, 2.2, 3); will now be vector3(1.5 , 2, 3); I have tried and also searched for an answer, I keep coming up empty.

**Answer** by Yamaraj
·
Jun 30, 2018 at 08:11 AM

Create this class:

```
static class ExtensionMethods
{
/// <summary>
/// Rounds Vector3.
/// </summary>
/// <param name="vector3"></param>
/// <param name="decimalPlaces"></param>
/// <returns></returns>
public static Vector3 Round(this Vector3 vector3, int decimalPlaces = 2)
{
float multiplier = 1;
for (int i = 0; i < decimalPlaces; i++)
{
multiplier *= 10f;
}
return new Vector3(
Mathf.Round(vector3.x * multiplier) / multiplier,
Mathf.Round(vector3.y * multiplier) / multiplier,
Mathf.Round(vector3.z * multiplier) / multiplier);
}
}
```

and now you can use it in whole project. Usage example:

```
vector3 = new Vector3(1.23243f, 2.213124f, 12.4123f);
var roundedVector3 = vector3.Round(1);
```

Nice one! I keep a collection of extension methods like this. I like solving a problem only once.

'Vector2' does not contain a definition for 'Round' accepting a fist argument of type 'Vector2' could be found (are you missing a using directive or an assembly reference?) I modified it to take Vector2 but it keeps giving me this error Please help thanks, Zimbwawa

```
public static Vector2 Round(this Vector2 vector, int decimalPlaces = 2)
{
float multiplier = 1;
for (int i = 0; i < decimalPlaces; i++)
{
multiplier *= 10f;
}
return new Vector2(
Mathf.Round(vector.x * multiplier) / multiplier,
Mathf.Round(vector.y * multiplier) / multiplier,
Mathf.Round(vector.z * multiplier) / multiplier);
}
```

**Answer** by robertbu
·
Dec 05, 2013 at 02:04 AM

```
var vec = Vector3(1.263, 2.2, 3);
vec *= 2.0;
vec = Vector3(Mathf.Round(vec.x), Mathf.Round(vec.y), Mathf.Round(vec.z));
vec /= 2.0;
```

Okay thank you kom and rob. Rob, can you explain what Each bidt does? (Exept setting the var.) Im not sure what vec *= 2 is for and all that. I under stand what lien 3 does though. also does that round to nearest .5?

This code rounds to the nearest .5. The second line can be rewritten as:

```
vec = vec * 2.0;
```

So it multiplies each of the components by 2.0. So the resulting vector would be (2.526, 4.4, 6);

Line 3 rounds the values, so the result is (3.0, 4.0, 6.0);

The last line can be rewritten as:

```
vec = vec / 2.0;
```

And the result at the (1.5, 2.0, 3).

Holy crap (Please escuse my language) Thank you!, I will test this out later.

More logical and clear would be first divide by .5 and then multiply by .5, because instead of ".5" there could be a variable.

I think robertbu's approach is actually more practical. You choose how many subdivisions you want in one unit. So a value of 2 splits one unit in 2 parts. A value of 3 splits it in 3 parts. If you use the reciprocal and want the 3 subdiv you would need to use 0.333333... The reciprocal of any value has the same meaning if you invert the operation. Just like the frequency and the wavelength are just the reciprocal of each other. However it's usually more practical to specity the frequency instead of the wavelength. It generally depends on the usecase though and none is more "correct"

**Answer** by komodor
·
Dec 05, 2013 at 01:54 AM

have you tried:

Vector3 newVector3 = new Vector3(Mathf.Round(oldVector3.x*10f)/10f, Mathf.Round(oldVector3.y*10f)/10f, Mathf.Round(oldVector3.z*10f)/10f)

?

edit: because Vector3 is struct and i don't see any simple way how to round it

**Answer** by GeekyMonkey
·
Aug 08, 2018 at 07:51 AM

Below are my Vector3 extension methods to snap a position to a grid. To solve your problem you'd do

```
vec = vec.SnapToGrid(0.5f);
```

The first one is just a simple snap, and the 2nd one allows you to offset your position from the grid. It first adds on an offset, then snaps, then reverses the offset.

```
public static class GmVector3Extensions
{
/// <summary>
/// Snap Vector3 to nearest grid position
/// </summary>
/// <param name="vector3">Sloppy position</param>
/// <param name="gridSize">Grid size</param>
/// <returns>Snapped position</returns>
public static Vector3 Snap(this Vector3 vector3, float gridSize = 1.0f)
{
return new Vector3(
Mathf.Round(vector3.x / gridSize) * gridSize,
Mathf.Round(vector3.y / gridSize) * gridSize,
Mathf.Round(vector3.z / gridSize) * gridSize);
}
/// <summary>
/// Snap Vector3 to nearest grid position with offset
/// </summary>
/// <param name="vector3">Sloppy position</param>
/// <param name="gridSize">Grid size</param>
/// <returns>Snapped position</returns>
public static Vector3 SnapOffset(this Vector3 vector3, Vector3 offset, float gridSize = 1.0f)
{
Vector3 snapped = vector3 + offset;
snapped = new Vector3(
Mathf.Round(snapped.x / gridSize) * gridSize,
Mathf.Round(snapped.y / gridSize) * gridSize,
Mathf.Round(snapped.z / gridSize) * gridSize);
return snapped - offset;
}
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.