The lodash _.round method compared to Math.round, and formating fun.
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.
1  what to know before hand
this is a post on the lodash method /_.round that can be used to round numbers in the same way as that of Math.round in native javaScript, but can also be used to round to a given precision as well. In this post I also expand into, and touch base on other relevant topics as well with respect to padding, and formating of numbers. This is not a getting started post on lodash, or javaScript in general and I assume you have at least a little background in these subjects.
2  Some basic examples of _.round, and Math.round.
So in core javaScript there is of course Math.round, as well as other options like Math.floor, and Math.ceil. These methods work just fine, but they only take one argument, which is naturally the number that you want to round. If you want to round to a certain precision, you will need to find a copy, and past solution. However if it just so happens that lodash is part of your stack, then there is no need, as you have a more robust rounding method at the ready that can receive a precision as the second argument.
2.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.
2.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.
3  format money _.round example
For this example I will be making a simple method that formats a plain javaScript number into a string that is both rounded, and padded using lodash methods.


4  Conclusion
So lodash methods like _.round are methods that have at least one little feature that makes it a little more useful then just the plain old native method in core javaScript itself. Still it goes without saying that this method alone is not a good reason to make lodash part of you stack. Hopefully you are making great use of the less redundant methods in lodash like _.merge, if it is part or your code base.