将逗号插入对象和输入文本字段的数字中 [英] Inserting Commas into numbers for both objects and input text fields

查看:225
本文介绍了将逗号插入对象和输入文本字段的数字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个使用这些公式开发的计算器:
// --- Math ---

  function national(){
x = Number($('#budget')。val());
y = Number($('#percentage')。val());

a = x * 2;
b = [x *(100 / 100-(1 / y))] * 2;
c =(x *(1 / y))* 4;
d =(b + c)-a;
e =(d / a)* 100;
f =(b + c);

$('#one')。text(Math.round(a));
$('#two')。text(Math.round(b));
$('#three')。text(Math.round(c));
$('#four')。text(Math.round(d));
$('#five')。text(Math.round(e));
$('#six')。text(Math.round(f));

$('#input1')。text(Math.round(y));
$('#input2')。text(Math.round(x));


}

我也无法更改格式很大程度上是因为我使用这个结构使用var作为伪数组来提供一个条形图插件,它以图形方式描述了我的计算。



我使用了Math.round动作来消除疯狂的计算,但是我需要在输出中添加数字格式。

我正在使用这个正则表达式插入我的逗号

  Number.prototype.format = function(f){
return this.toString()。split(/(?=(?:\\\{$))+(?:\.|$))/g).join(,);
// ex $('#one')。text(a.format());
};

但我不知道如何结合正则表达式和Math.round动作...他们保留杀死对方。

问题:
我需要格式化输出DOM对象和文本输入字段。这是甚至可能的,我的语法根本不合适,任何输入或可能的解决方案,将不胜感激。
$ b

奖金:
我'使用jQuery来执行我正在被转储到跨度的计算,如果有一个合适的解决方案,我不仅可以将所述解决方案合并到我的输出跨度,而且我的输入文本字段以及?

干杯。

解决方案

使用这个:

<$ ()函数返回this.toString()。replace(/(\ d)(?=(\ d {3}))pre> Number.prototype.format = function(){
+ $)/ g,'$ 1');
};

然后您可以调用 .format()数学运算:

  $('#one')。text(Math.round(a).format()); 
$('#two')。text(Math.round(b).format());
$('#three')。text(Math.round(c).format());
$('#four')。text(Math.round(d).format());
$('#five')。text(Math.round(e).format());
$('#six')。text(Math.round(f).format());

$('#input1')。text(Math.round(y).format());
$('#input2')。text(Math.round(x).format());


Situation: I have a calculator that I'm developing using these formulas: // --- Math ---

function national(){
        x = Number($('#budget').val());
        y = Number($('#percentage').val());

        a = x * 2;
        b = [x*(100/100-(1/y))]*2;
        c = (x*(1/y))*4;
        d = (b+c)-a;
        e = (d/a)*100;
        f = (b+c);

        $('#one').text(Math.round(a));
        $('#two').text(Math.round(b));
        $('#three').text(Math.round(c));
        $('#four').text(Math.round(d));
        $('#five').text(Math.round(e));
        $('#six').text(Math.round(f));

        $('#input1').text(Math.round(y));
        $('#input2').text(Math.round(x)); 


    }

I can't change the formatting too much because I'm using this structure use a var as a pseudo-array to feed a bar graph plugin that graphically depicts my calculations.

I have used the Math.round action to eliminate crazy calculations, but I need to add numerical formatting to the output.

I was using this regex to inset my commas

Number.prototype.format = function (f) {
    return this.toString().split( /(?=(?:\d{3})+(?:\.|$))/g ).join( "," );
// ex $('#one').text(a.format());
};

but I don't know how to incorporate the regex AND Math.round action...they keep killing each other.

Problem: I need to format both my output DOM objects and my text input fields. Is this even possible and my syntax simply not appropriate, any input or possible solutions would be appreciated.

Bonus: I'm using jQuery to perform my calculations which are being dumped into spans, if there is a proper solution, could I not only incorporate said solution in my output spans, but my input text fields as well?

Cheers.

解决方案

Use this:

Number.prototype.format = function () {
    return this.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
};

Then you could just call .format() after rounding:

$('#one').text(Math.round(a).format());
$('#two').text(Math.round(b).format());
$('#three').text(Math.round(c).format());
$('#four').text(Math.round(d).format());
$('#five').text(Math.round(e).format());
$('#six').text(Math.round(f).format());

$('#input1').text(Math.round(y).format());
$('#input2').text(Math.round(x).format());

这篇关于将逗号插入对象和输入文本字段的数字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆