Feature request : toFixed() for vectors value
See original GitHub issueFeature request
It would be nice to add a parameter to Vector3.round()
and Vector3.roundToZero()
, to ask for a number of decimal like Number.toFixed()
in JS. If undefined, it would assume that an integer is needed, so it would not break anything.
Use case
I had to do my own function to do that, because the objects in my scene are user-transformed, then the transforms are exported as JSON. There is a big tolerance, so in order to reduce file size, I round the vector3 values. But rounding to integer is too much, that’s why I did my own function. I guess that it would also be useful in things like voxel-painter or Minecraft-likes.
I thought it might not be a big deal to add it to three.js, and it’s useful, hence my feature request. If you think it’s useless code, please close the issue.
Issue Analytics
- State:
- Created 4 years ago
- Comments:19 (6 by maintainers)
Top GitHub Comments
We should not use
Number.toFixed()
since it returns a string. It seems that number-to-string conversion just for rounding is no good approach.https://stackoverflow.com/questions/2283566/how-can-i-round-a-number-in-javascript-tofixed-returns-a-string#comment47147413_14978830
Can you please test if this would work for you:
Live example (check browser console): https://glitch.com/~meowing-bougon
However, I don’t think this logic makes sense for
Vector3.roundToZero()
.The semantics of
Number.toFixed()
andVector3.toFixed()
would be different since the native function returns a string. Hence, it’s confusing to reuse the same name.I think enhancing the methods is okay. If we can solve the issue @sciecode mentioned.