有没有办法将数字四舍五入为读者友好的格式? (例如 1100 美元)

发布于 2024-08-29 22:28:00 字数 1536 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-09-05 22:28:00

这是一个简单的函数来做到这一点:

function abbrNum(number, decPlaces) {
  // 2 decimal places => 100, 3 => 1000, etc
  decPlaces = Math.pow(10, decPlaces);

  // Enumerate number abbreviations
  var abbrev = ["k", "m", "b", "t"];

  // Go through the array backwards, so we do the largest first
  for (var i = abbrev.length - 1; i >= 0; i--) {

    // Convert array index to "1000", "1000000", etc
    var size = Math.pow(10, (i + 1) * 3);

    // If the number is bigger or equal do the abbreviation
    if (size <= number) {
      // Here, we multiply by decPlaces, round, and then divide by decPlaces.
      // This gives us nice rounding to a particular decimal place.
      number = Math.round(number * decPlaces / size) / decPlaces;

      // Handle special case where we round up to the next abbreviation
      if ((number == 1000) && (i < abbrev.length - 1)) {
        number = 1;
        i++;
      }

      // Add the letter for the abbreviation
      number += abbrev[i];

      // We are done... stop
      break;
    }
  }

  return number;
}
console.log(abbrNum(1200, 3))

输出:

abbrNum(12 , 1)          => 12
abbrNum(0 , 2)           => 0
abbrNum(1234 , 0)        => 1k
abbrNum(34567 , 2)       => 34.57k
abbrNum(918395 , 1)      => 918.4k
abbrNum(2134124 , 2)     => 2.13m
abbrNum(47475782130 , 2) => 47.48b

演示:http://jsfiddle.net/jtbowden/SbqKL/

Here is a simple function to do it:

function abbrNum(number, decPlaces) {
  // 2 decimal places => 100, 3 => 1000, etc
  decPlaces = Math.pow(10, decPlaces);

  // Enumerate number abbreviations
  var abbrev = ["k", "m", "b", "t"];

  // Go through the array backwards, so we do the largest first
  for (var i = abbrev.length - 1; i >= 0; i--) {

    // Convert array index to "1000", "1000000", etc
    var size = Math.pow(10, (i + 1) * 3);

    // If the number is bigger or equal do the abbreviation
    if (size <= number) {
      // Here, we multiply by decPlaces, round, and then divide by decPlaces.
      // This gives us nice rounding to a particular decimal place.
      number = Math.round(number * decPlaces / size) / decPlaces;

      // Handle special case where we round up to the next abbreviation
      if ((number == 1000) && (i < abbrev.length - 1)) {
        number = 1;
        i++;
      }

      // Add the letter for the abbreviation
      number += abbrev[i];

      // We are done... stop
      break;
    }
  }

  return number;
}
console.log(abbrNum(1200, 3))

Outputs:

abbrNum(12 , 1)          => 12
abbrNum(0 , 2)           => 0
abbrNum(1234 , 0)        => 1k
abbrNum(34567 , 2)       => 34.57k
abbrNum(918395 , 1)      => 918.4k
abbrNum(2134124 , 2)     => 2.13m
abbrNum(47475782130 , 2) => 47.48b

Demo: http://jsfiddle.net/jtbowden/SbqKL/

忆梦 2024-09-05 22:28:00
var floor=Math.floor, abs=Math.abs, log=Math.log, round=Math.round, min=Math.min;
var abbrev = ['k', 'Mil', 'Bil']; // abbreviations in steps of 1000x; extensible if need to edit

function rnd(n, precision) {
    var prec = 10**precision;
    return round(n*prec)/prec;
}

function format(n) {
    var base = floor(log(abs(n))/log(1000));
    var suffix = abbrev[min(abbrev.length-1, base-1)];
    base = abbrev.indexOf(suffix) + 1;
    return suffix ? rnd(n/1000**base,2)+suffix : ''+n;
}

演示:

> tests = [-1001, -1, 0, 1, 2.5, 999, 1234, 
           1234.5, 1000001, 10**9, 10**12]
> tests.forEach(function(x){ console.log(x,format(x)) })

-1001 "-1k"
-1 "-1"
0 "0"
1 "1"
2.5 "2.5"
999 "999"
1234 "1.23k"
1234.5 "1.23k"
1000001 "1Mil"
1000000000 "1Bil"
1000000000000 "1000Bil"
var floor=Math.floor, abs=Math.abs, log=Math.log, round=Math.round, min=Math.min;
var abbrev = ['k', 'Mil', 'Bil']; // abbreviations in steps of 1000x; extensible if need to edit

function rnd(n, precision) {
    var prec = 10**precision;
    return round(n*prec)/prec;
}

function format(n) {
    var base = floor(log(abs(n))/log(1000));
    var suffix = abbrev[min(abbrev.length-1, base-1)];
    base = abbrev.indexOf(suffix) + 1;
    return suffix ? rnd(n/1000**base,2)+suffix : ''+n;
}

Demo:

> tests = [-1001, -1, 0, 1, 2.5, 999, 1234, 
           1234.5, 1000001, 10**9, 10**12]
> tests.forEach(function(x){ console.log(x,format(x)) })

-1001 "-1k"
-1 "-1"
0 "0"
1 "1"
2.5 "2.5"
999 "999"
1234 "1.23k"
1234.5 "1.23k"
1000001 "1Mil"
1000000000 "1Bil"
1000000000000 "1000Bil"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文