是否有任何人都知道的用于格式化“缩写”的良好 JavaScript 片段?数字?

发布于 2024-11-17 00:20:43 字数 418 浏览 3 评论 0原文

关键是缩写。例如,用 1m 代替 1000000,用 12k 代替 12000 等 - 很像 StackOverflow!

除了我尝试过的之外,我不确定还需要添加什么:

格式数字缩写 javascript
formatnumbersshortjavascript

还有其他一些搜索和搜索结果,但没有运气。我觉得有人以前一定做过这样的事,讨厌重新发明轮子之类的!

干杯

编辑:我正在寻找一个数字,即12345并将其转换为12k

抱歉我不太清楚!

The key is abbreviated. For example, 1m instead of 1000000, and 12k instead of 12000 etc. - much like on StackOverflow!

I'm not sure what else to add other than I've tried:

format numbers abbreviated javascript
format numbers short javascript

And a few other searches and scoured the results with no luck. I feel like someone must have done this before, hate reinventing wheels and all that!

Cheers

Edit: I'm looking to take a number, i.e. 12345 and turn it into 12k

Sorry I wasn't very clear!

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

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

发布评论

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

评论(3

愛上了 2024-11-24 00:20:43

这是我很久以前编写的一些代码,但它运行良好。它甚至支持小数。

function is_numeric(string) {
    for(var i = 0; i < string.length; i++) {
        if(string.charAt(i) < '0' || string.charAt(i) > '9') {
            return false;
        }
    }
    return true;
}

function charValueMultiplier(letter) {
    switch(letter) {
        case 'M':
        case 'm': return 1000000;
        case 'k':
        case 'K': return 1000;
        default: return 0;
    }
}

// parse string like 1.5M or 10k and return the number
function parseNumber(string) {
    string = string.replace(/ /g, ''); // remove spaces
    var total           = 0;
    var partial         = 0;
    var partialFraction = 0;
    var fractionLength  = 0;
    var isFraction      = false;

    // process the string; update total if we find a unit character
    for(var i = 0; i < string.length; i++) {
        var c = string.substr(i, 1);
        if(c == '.' || c == ',') {
            isFraction = true;
        }
        else if(is_numeric(c)) {
            if(isFraction) {
                partialFraction = partialFraction * 10 + parseInt(c, 10);
                fractionLength++;
            }
            else {
                partial = partial * 10 + parseInt(c, 10);
            }
        }
        else {
            total += charValueMultiplier(c) * partial + charValueMultiplier(c) * partialFraction / Math.pow(10, fractionLength);

            partial         = 0;
            partialFraction = 0;
            fractionLength  = 0;
            isFraction      = false;
        }
    }

    return Math.round(total + partial + partialFraction / Math.pow(10, fractionLength));
}

Here's some code I've written quite some time ago but it works fine. It even supports decimals.

function is_numeric(string) {
    for(var i = 0; i < string.length; i++) {
        if(string.charAt(i) < '0' || string.charAt(i) > '9') {
            return false;
        }
    }
    return true;
}

function charValueMultiplier(letter) {
    switch(letter) {
        case 'M':
        case 'm': return 1000000;
        case 'k':
        case 'K': return 1000;
        default: return 0;
    }
}

// parse string like 1.5M or 10k and return the number
function parseNumber(string) {
    string = string.replace(/ /g, ''); // remove spaces
    var total           = 0;
    var partial         = 0;
    var partialFraction = 0;
    var fractionLength  = 0;
    var isFraction      = false;

    // process the string; update total if we find a unit character
    for(var i = 0; i < string.length; i++) {
        var c = string.substr(i, 1);
        if(c == '.' || c == ',') {
            isFraction = true;
        }
        else if(is_numeric(c)) {
            if(isFraction) {
                partialFraction = partialFraction * 10 + parseInt(c, 10);
                fractionLength++;
            }
            else {
                partial = partial * 10 + parseInt(c, 10);
            }
        }
        else {
            total += charValueMultiplier(c) * partial + charValueMultiplier(c) * partialFraction / Math.pow(10, fractionLength);

            partial         = 0;
            partialFraction = 0;
            fractionLength  = 0;
            isFraction      = false;
        }
    }

    return Math.round(total + partial + partialFraction / Math.pow(10, fractionLength));
}
倾`听者〃 2024-11-24 00:20:43

我制作了一个 npm 包,可以为您执行此操作: https://www.npmjs.com/ package/approximate-number

用于 Node.js(或通过 Browserify 的浏览器):

npm install --save approximate-number

然后在您的代码中:

var approx = require('approximate-number');
approx(123456); // "123k" 

或者,通过 Bower 在浏览器中使用:

bower install approximate-number

然后

window.approximateNumber(123456); // "123k" 

I made an npm package that can do this for you: https://www.npmjs.com/package/approximate-number

Usage for Node.js (or browsers via Browserify):

npm install --save approximate-number

and then in your code:

var approx = require('approximate-number');
approx(123456); // "123k" 

Or, for usage in browsers via Bower:

bower install approximate-number

And then

window.approximateNumber(123456); // "123k" 
沧桑㈠ 2024-11-24 00:20:43

如果我理解正确的话,您一个数字n并且想要将其格式化为字符串。然后

// n being the number to be formatted
var s = "" + n; // cast as string
if (n >= 1000000) {
  s = s.substring(0, s.length - 6) + "M";
} else if (n >= 1000) {
  s = s.substring(0, s.length - 3) + "k";
}

应该做该工作。您当然可以根据您的需要进行扩展,甚至可以包含数字 < 1..

If I understand correctly, you have a number n and want to format it to a string. Then

// n being the number to be formatted
var s = "" + n; // cast as string
if (n >= 1000000) {
  s = s.substring(0, s.length - 6) + "M";
} else if (n >= 1000) {
  s = s.substring(0, s.length - 3) + "k";
}

should do the job. You can of course extend it to your needs or even include numbers < 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文