Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript将数字转换为不同的格式或字符串替代_Javascript_Jquery - Fatal编程技术网

Javascript将数字转换为不同的格式或字符串替代

Javascript将数字转换为不同的格式或字符串替代,javascript,jquery,Javascript,Jquery,更新: 使用javascript或jQuery,如何将数字转换为不同的变体: 例如: 一百万 到 或 一千 到 或 1934年和1234年 到 或 这可以在函数中完成吗 希望这有意义 理论上,是的 正如TimWolla指出的,这需要很多逻辑 RubyonRails有一个用文字表示时间的助手。看一看这张照片。该代码的实现是,并且可以为您提供一些关于如何实现该代码的提示 我同意通过选择一种格式来降低复杂性的意见 希望你能在我的回答中找到一些帮助。理论上,是的 正如TimWolla指出的,这需要很多逻

更新:

使用javascript或jQuery,如何将数字转换为不同的变体:

例如:

一百万 到

一千 到

1934年和1234年 到

这可以在函数中完成吗

希望这有意义

理论上,是的

正如TimWolla指出的,这需要很多逻辑

RubyonRails有一个用文字表示时间的助手。看一看这张照片。该代码的实现是,并且可以为您提供一些关于如何实现该代码的提示

我同意通过选择一种格式来降低复杂性的意见

希望你能在我的回答中找到一些帮助。

理论上,是的

正如TimWolla指出的,这需要很多逻辑

RubyonRails有一个用文字表示时间的助手。看一看这张照片。该代码的实现是,并且可以为您提供一些关于如何实现该代码的提示

我同意通过选择一种格式来降低复杂性的意见


希望您能在我的答案中找到一些帮助。

您可以将方法添加到
Number.prototype
,例如:

Number.prototype.addCommas = function () {
    var intPart = Math.round(this).toString();
    var decimalPart = (this - Math.round(this)).toString();
    // Remove the "0." if it exists
    if (decimalPart.length > 2) {
        decimalPart = decimalPart.substring(2);
    } else {
        // Otherwise remove it altogether
        decimalPart = '';
    }
    // Work through the digits three at a time
    var i = intPart.length - 3;
    while (i > 0) {
        intPart = intPart.substring(0, i) + ',' + intPart.substring(i);
        i = i - 3;
    }
    return intPart + decimalPart;
};
现在您可以将其称为
var num=1000;num.addCommas()
,它将返回
“1000”
。这只是一个示例,但是您会发现创建的所有函数都将涉及在过程的早期将数字转换为字符串,然后处理并返回字符串。(分隔整数和小数部分可能特别有用,因此您可能希望将其重构为自己的方法。)希望这足以让您开始学习

编辑:下面是如何做K的事情。。。这个比较简单:

Number.prototype.k = function () {
    // We don't want any thousands processing if the number is less than 1000.
    if (this < 1000) {
        // edit 2 May 2013: make sure it's a string for consistency
        return this.toString();
    }
    // Round to 100s first so that we can get the decimal point in there
    // then divide by 10 for thousands
    var thousands = Math.round(this / 100) / 10;
    // Now convert it to a string and add the k
    return thousands.toString() + 'K';
};
Number.prototype.k=函数(){
//如果数字小于1000,我们不希望有数千个处理。
如果(该值小于1000){
//编辑2013年5月2日:确保为一致性字符串
返回此.toString();
}
//先四舍五入到100,这样我们可以得到小数点
//然后用10除以千
var千=数学四舍五入(此/100)/10;
//现在将其转换为字符串并添加k
返回数千.toString()+'K';
};

用同样的方法调用它:
var num=2000;num.k()
您可以将方法添加到
Number.prototype
,例如:

Number.prototype.addCommas = function () {
    var intPart = Math.round(this).toString();
    var decimalPart = (this - Math.round(this)).toString();
    // Remove the "0." if it exists
    if (decimalPart.length > 2) {
        decimalPart = decimalPart.substring(2);
    } else {
        // Otherwise remove it altogether
        decimalPart = '';
    }
    // Work through the digits three at a time
    var i = intPart.length - 3;
    while (i > 0) {
        intPart = intPart.substring(0, i) + ',' + intPart.substring(i);
        i = i - 3;
    }
    return intPart + decimalPart;
};
现在您可以将其称为
var num=1000;num.addCommas()
,它将返回
“1000”
。这只是一个示例,但是您会发现创建的所有函数都将涉及在过程的早期将数字转换为字符串,然后处理并返回字符串。(分隔整数和小数部分可能特别有用,因此您可能希望将其重构为自己的方法。)希望这足以让您开始学习

编辑:下面是如何做K的事情。。。这个比较简单:

Number.prototype.k = function () {
    // We don't want any thousands processing if the number is less than 1000.
    if (this < 1000) {
        // edit 2 May 2013: make sure it's a string for consistency
        return this.toString();
    }
    // Round to 100s first so that we can get the decimal point in there
    // then divide by 10 for thousands
    var thousands = Math.round(this / 100) / 10;
    // Now convert it to a string and add the k
    return thousands.toString() + 'K';
};
Number.prototype.k=函数(){
//如果数字小于1000,我们不希望有数千个处理。
如果(该值小于1000){
//编辑2013年5月2日:确保为一致性字符串
返回此.toString();
}
//先四舍五入到100,这样我们可以得到小数点
//然后用10除以千
var千=数学四舍五入(此/100)/10;
//现在将其转换为字符串并添加k
返回数千.toString()+'K';
};

用同样的方法调用它:
var num=2000;num.k()

我不认为有一个完整的解决方案,因为这将需要大量的logik。特别是:刚刚低于*好的,谢谢。。如果没有刚过的stuf呢?我不认为有一个完整的解决方案,因为这需要一个巨大的logik。特别是:刚刚低于*好的,谢谢。。如果没有“刚下/刚过”按钮呢?你可以在这里找到一种格式化数字的方法:谢谢你的链接。。。我到处玩,但没有什么能完全满足我的需要。看起来不错的那个似乎有错误。你可以在这里找到格式化数字的方法:谢谢你的链接。。。我到处玩,但没有什么能完全满足我的需要。看起来不错的那个似乎有错误。。这是可行的,但是我如何将K添加到如下格式:
27768到:27.7K
谢谢。快速提问,作为一个函数原型比函数声明更有效吗?有区别吗?没有太大区别。不管你喜欢哪种方式都行。。这是可行的,但是我如何将K添加到如下格式:
27768到:27.7K
谢谢。快速提问,作为一个函数原型比函数声明更有效吗?有区别吗?没有太大区别。不管你喜欢哪种方式都可以。
Number.prototype.addCommas = function () {
    var intPart = Math.round(this).toString();
    var decimalPart = (this - Math.round(this)).toString();
    // Remove the "0." if it exists
    if (decimalPart.length > 2) {
        decimalPart = decimalPart.substring(2);
    } else {
        // Otherwise remove it altogether
        decimalPart = '';
    }
    // Work through the digits three at a time
    var i = intPart.length - 3;
    while (i > 0) {
        intPart = intPart.substring(0, i) + ',' + intPart.substring(i);
        i = i - 3;
    }
    return intPart + decimalPart;
};
Number.prototype.k = function () {
    // We don't want any thousands processing if the number is less than 1000.
    if (this < 1000) {
        // edit 2 May 2013: make sure it's a string for consistency
        return this.toString();
    }
    // Round to 100s first so that we can get the decimal point in there
    // then divide by 10 for thousands
    var thousands = Math.round(this / 100) / 10;
    // Now convert it to a string and add the k
    return thousands.toString() + 'K';
};