Round to the nearest xx50

Hi!

I’m using Unreal 5.3.2 with Blueprints.

I’m developing a Blueprint algorithm that, given a float number as input, it returns its closest value to number ending in 50. Sorry, i don’t know how to explain it better. With examples I mean, I need these numbers: 50, 150, 250, 350, 450, 550, etc.

In other words, if the input value is between, i.e., [1050, 1100), it will return 1050, but
if the value is between [1100, 1150], it will return 1150.

So, this is my algorithm with only works when input values are equal or greater than 1000.0:

  1. Divide input value by 100.0.
  2. Take fraction part
  3. If faction part is greater or equal to 0.0 and less than 0.5 then add one to integer part.
  4. Multiply integer part by 100.
  5. To the result add 50.

When the input is 701.272 it returns 850.0, but it should return 750.0.

Maybe the problem is divide and multiple by 100 if the input number is less than 1000.

How can I fix this algorithm?

Thanks.

You are looking for function called GridSnap, that does exactly what you looking for

2 Likes

Thanks for your answer, but maybe I’m not using Snap to Grip correctly.

imagen

I have tried with 50.0, 100.0 and 150.0 as Grid Size, but it doesn’t return the value that I’m looking for. For example, with Grid Size equals to 150.0 and location equals to 813.197, this node returns 750.0, but it should returns 850.0.

I think I should move the pivot point 50 units to the left:

But, I don’t want to do it. Or maybe, this is not the problem.

By the way, the size of that mesh is 100x200x300.

1 Like

With example of grid of 150, the grid positions are: […, -150, 0, 150, 300, 450, 600, 750, 900, …], here 750 is indeed the closest value to 813.

In your example the desired grid is [50, 150, 250, 350, 450, 550], which we can see have a step of 100 - it’s your GridSize. And since it’s starts not from 0, but from 50 - just offset the node output by 50 (literally add 50(or subtract, depending on goal))

1 Like

This is how I solved it:

I hope you aware about difference between truncate() & round(), since as is it isn’t “nearest”. though for some tasks this will be accurate enough

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.