Hi! for example, if I have 273 how do I round it to 270?

By using Math?

Divide your value by 10, then put it on a integer.

And then multiply it by 10.

Example:

48 / 10 = 4.8 → (int) 4

4 * 10 = 40

hahaha that was a dirty trick

Dividing by 10 and then rounding it (ceil, floor or just round, depending on what you want to do with it) before multiplying again seems like a better idea to me. The nearest 10 to 48 should be 50, not 40.

Yes, I agree

+1

Better solution

thanks cayou66! you are a true Math wiz!

Ahah, is more logic than pure Math Glad it’s what you want.

But, as c-Row said, try to use ceil floor or roud rather than put it on a int. That’s a smarter way

I’ve used this trick for a couple of decades, you should make note of it if you ever want to do grid based snapping

The great magic is revealed

From when I used to code in BASIC: Int(number / 10 + 0.5) * 10

I can write the 6502 or Z80 assembler as well if you like.

Heres the formula i used in javascript.

y = 273;

x = parseInt(y / 10);

z = ((y / 10) - x) * 10;

if(10 - z < 5)

{y += (10 - z);}

else

{y -= z;}

hope this helps

I know this is an old post, but it’s the first thing that came up when I googled this problem so I thought I’d post the extension class I made for it here. Then just use it like

`floatValue.ToNearestMultiple(10);`

or

`floatValue.ToNearestMultiple(10, FloatExtensions.ROUNDING.UP);`

```
public static class FloatExtensions
{
public enum ROUNDING { UP, DOWN, CLOSEST }
public static float ToNearestMultiple(this float f, int multiple, ROUNDING roundTowards = ROUNDING.CLOSEST)
{
f /= multiple;
return (roundTowards == ROUNDING.UP ? Mathf.Ceil(f) : (roundTowards == ROUNDING.DOWN ? Mathf.Floor(f) : Mathf.Round(f))) * multiple;
}
/// <summary>
/// Using a multiple with a maximum of two decimal places, will round to this value based on the ROUNDING method chosen
/// </summary>
public static float ToNearestMultiple(this float f, float multiple, ROUNDING roundTowards = ROUNDING.CLOSEST)
{
f = float.Parse((f * 100).ToString("f0"));
multiple = float.Parse((multiple * 100).ToString("f0"));
f /= multiple;
f = (roundTowards == ROUNDING.UP ? Mathf.Ceil(f) : (roundTowards == ROUNDING.DOWN ? Mathf.Floor(f) : Mathf.Round(f))) * multiple;
return f / 100;
}
}
```

Thankssss Aaaron. Works perfectly. It snap to 10 increment

float x=15.6f;

float x2=x%10;

float x3=x-x2;

if(x>0){

if(x2>5){

x3+=10;

}

}

else if(x<0){

if(x2<5){

x3-=10;

}

}

Debug.Log(x3);

This is so useful, thank you so much for sharing