字符串用 JavaScript 固定?

发布于 2024-11-10 02:59:43 字数 1006 浏览 1 评论 0原文

我有一个数字,我需要将其格式化为货币,为此,我必须将我的数字转换为字符串并运行一个函数,这是有效的,但它显示为 X 位小数,是否可以在字符串吗?我尝试过,但没有运气,我不确定如何将字符串转回数字,我只使用了 parseInt 它停在第一个字符,因为它没有读取超过我的分隔符...

var amount = String(EstimatedTotal);
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
    var nn = n.substr(n.length-3);
    a.unshift(nn);
    n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;

目前 amount 变量显示为1,234,567.890123

感谢大家的帮助,

设法让它与此一起工作

amount = String(EstimatedTotal) 

 amount += '';
x = amount.split('.');
x1 = x[0];
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
num=x1 ;

I have a number which I needed to format as currency, to do this im having to turn my number into a string and run a function, This is working but its showing to X decimal places, Is it possible to use 'toFixed' on a string at all? I've tried with no luck and im unsure how to turn the string back into a number, I've used parseInt only it stops at the first character as it doesnt read past my delimeter...

var amount = String(EstimatedTotal);
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
    var nn = n.substr(n.length-3);
    a.unshift(nn);
    n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;

at the moment the amount variable showing as 1,234,567.890123

Thanks for the help all ,

Managed to get it working with this

amount = String(EstimatedTotal) 

 amount += '';
x = amount.split('.');
x1 = x[0];
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
num=x1 ;

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

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

发布评论

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

评论(2

灯角 2024-11-17 02:59:43

不确定您所说的货币是什么意思 -

这会为千位分隔符添加逗号,并强制输入两位小数,

输入可以是带或不带逗号的数字字符串、减号和小数,或者是数字

function addCommas2decimals(n){
    n= Number(String(n).replace(/[^-+.\d]+/g, ''));
    if(isNaN(n)){
        throw new Error('Input must be a number');
    }
    n= n.toFixed(2);
    var rx=  /(\d+)(\d{3})/;
    return n.replace(/^\d+/, function(w){
        while(rx.test(w)){
            w= w.replace(rx, '$1,$2');
        }
        return w;
    });

}
var s= '1234567.890123'
addCommas2decimals(s)

/*  returned value: (String)
1,234,567.89
*/

Not sure what you mean by currency-

this adds commas for thousands separators and forces two decimal places

input can be a string of digits with or without commas, minus sign and decimal, or a number

function addCommas2decimals(n){
    n= Number(String(n).replace(/[^-+.\d]+/g, ''));
    if(isNaN(n)){
        throw new Error('Input must be a number');
    }
    n= n.toFixed(2);
    var rx=  /(\d+)(\d{3})/;
    return n.replace(/^\d+/, function(w){
        while(rx.test(w)){
            w= w.replace(rx, '$1,$2');
        }
        return w;
    });

}
var s= '1234567.890123'
addCommas2decimals(s)

/*  returned value: (String)
1,234,567.89
*/
红墙和绿瓦 2024-11-17 02:59:43

我推荐 phpjs 项目中的“number_format”函数:

http://phpjs.org/functions/number_format:481

用法:

amount = number_format(EstimatedTotal, 2, '.', ',');

就像 PHP 函数一样... http://php.net/manual/en/function.number-format.php

I recommend the "number_format" function from the phpjs project:

http://phpjs.org/functions/number_format:481

Usage:

amount = number_format(EstimatedTotal, 2, '.', ',');

Just like the PHP function... http://php.net/manual/en/function.number-format.php

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