I was wondering if it is possible to get the approximate number in Javascript? Like if I had a large number for example
var number = 537989;
A bit like the Math.round() , just so it rounds up to 538000. Is there a way to do so? Thanks.
I was wondering if it is possible to get the approximate number in Javascript? Like if I had a large number for example
var number = 537989;
A bit like the Math.round() , just so it rounds up to 538000. Is there a way to do so? Thanks.
Share Improve this question asked Jan 4, 2014 at 20:41 MathiasMathias 1811 gold badge3 silver badges13 bronze badges 3-
2
Did you try dividing by
10,000
, then applyinground()
? – Frédéric Hamidi Commented Jan 4, 2014 at 20:43 - @frederichamidi why did you delete the answer? – Ruan Mendes Commented Jan 4, 2014 at 20:45
- 1 The answer was deleted by its owner. – robertklep Commented Jan 4, 2014 at 20:48
2 Answers
Reset to default 6You can write it yourself. Something like
function myround(number, precision = 1000) {
var result = Math.round(number / precision) * precision;
return result;
}
I wrote a JS library, approximate-number, that does this and a bit more. For example, 537989
would get approximated to "537k"
by default or "538k"
if you set round: true
.