sodiray / radash

Functional utility library - modern, simple, typed, powerful
https://radash-docs.vercel.app
MIT License
4.36k stars 174 forks source link

Add a Round function #61

Open ianrios opened 2 years ago

ianrios commented 2 years ago

We need a round function to mimic the lodash round function (allows optional fixed rounding decimal place precision)

example definition: _.round(n: number, p?: number)

This would be added to the numbers category in the docs.

lodash reference: https://lodash.com/docs/4.17.15#round

My current workaround, with JS Math.round, Math.pow, and radash toFloat:

const num = 123.456
const precision = 2
const mult = Math.pow(10, precision)
const rounded = Math.round(num * mult) / mult
const fixed = _.toFloat(rounded.toFixed(precision))
// => 123.46

My Ideal workflow using only radash round:

const num = 123.456
const precision = 2
const rounded = _.round(num, precision)
// => 123.46
sodiray commented 2 years ago

Hey @ianrios thanks for bringing this up 👏 I think round would be a great addition to radash. I think it's a pretty quick add as well.

shan-shaji commented 2 years ago

@rayepps , Can i work on this