The lodash _.round method compared to Math.round, and formating fun.
1 - what to know before hand
1.1 - Just simple rounding, with _.round, and Math.round.
If you just want to round a number then _.round is no different then Math.round in that regard, and there is not much of a difference.
Rounding works following the convention that a fraction of one half or higher is rounded up to the next whole number, and any fraction lower is rounded down. If you want to always round down no matter what there is _.floor, and _.ceil, as wel as the corresponding Math.floor, and Math.ceil as well.
1.2 - Rounding too a certain precision
The one feature that makes _.round, a little more robust is there a second argument can be given to set the precision of the number.
This is useful when it comes rounding a number that has to do with money to just two decimals. However when it comes to formating a number for presentation to the user it will not do everything when it comes formating numbers. To help with this there are padding methods like _.padStart.
2 - format money _.round example
So when it comes to using lodash just for this method alone that makes using the full lodash library kind of silly. It might be possible to just install the lodash round method alone as one way of going about addressing that. However it should not be to hard to work out or find some kind of user space solution for this sort of thing when it comes to roi8nding numbers. There are of course the Math methods that I have mentioned that should work just fine in most cases. Also it is not so hard to create a simple expression to address the lack of the precision argument in these methods. So then in this section I will be looking into at least a few examples that have to do with rounding numbers without the use of lodash, choosing to work with just what there is to deal with in core javaScriot itself.
3.1 - The Math round method
First off there is the round method of the Math object of course that will round a number to a whole number. This will work in the usual way where one half or higher will round up, while anything below one half will round down.
3.2 - Math ceil, and Math.floor
In some cases I might want to always round up, or always round down, for this there is Math.ceil and Math.floor. These methods work more or less the same way as Math.round in which I just pass the number I want to round as the first argument, and get a returned value that is the rounded number. However floor will always round down, and ceil will always round up.
3.3 - Using the toFixed method of a number with parseFloat
There is also the toFixed method of the number prototype that will return a string form of a number to a given number of fixed decimal points. This string result can then be feed back to a method like parseFloat to convert it back to a number again.
Although the toFixed method will work okay in most situations there are some rare, but possible situations in which one might get unexpected results. With that said if this kind of situation does present a problem this will require one to look into yet even more solutions for this kind of thing.
3.4 - User space round method
Well after doing some digging I as able to find this rounding solution that seems to work okay, however I have not gone threw every possible number example to see if there is some kind of weir problem with it.
4 - Conclusion
If you enjoyed this post you might want to check out one of my many other posts on lodash, or my main post on lodash in general.