I advise to review my previously posted code snippet.
FVector snap(FVector arg, float gridSize){
auto f = &](float val){
return gridSize ? (float)round(val/gridSize)*gridSize: val;
};
return FVector{f(arg.X), f(arg.Y), f(arg.Z)};
}
First, code duplication should be eliminated in almost all cases. You have three if/else branches doing the same thing, but on different fields of the structure. This stuff belong to another function or lambda. Duplication results in bugs and increases maintenance costs.
Also, cmath library has round() function, which rounds float/double value to the nearest integer.
Your code does the same thing as this builtin function, so you’re reinventing the wheel. That is a bad practice.
Using round(), you can replace convoluted if/else with
snappedValue = (float)round(val/gridSize)*gridSize;
It’s advised to use UE4 equivalents though. FMath::Round for example or FMath::GridSnap. That stuff is optimized for the engine and also provides the necessary platform independence (see FPlatformMath).