最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

integer - Javascript - Approximate Number - Stack Overflow

programmeradmin0浏览0评论

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 applying round()? – 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
Add a ment  | 

2 Answers 2

Reset to default 6

You 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.

发布评论

评论列表(0)

  1. 暂无评论