So today for yet another on eof my posts on lodash and corresponding topics I have come around to writing a quick post on the _.round method that can be used in a simular way to that of Math.round, bit with just one little additional feature that I just which the native methods had but does not. Also in this post I will be writing about some related topics that have to do with formating numbers, a common use case example that involves rounding.
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.
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.