Including Exponents in jQuery?

最后都变了- 提交于 2019-12-11 04:16:35

问题


How do you implement exponents in JQuery?

I'm modifying this:

$.fn.sumValues = function() {
var sum = 0; 
this.each(function() {
    if ( $(this).is(':input') ) {
        var val = $(this).val();
    } else {
        var val = $(this).text();
    }
    sum += parseFloat( ('0' + val).replace(/[^0-9-\.]/g, ''), 10 );
});
return sum;

and

$(document).ready(function() {
$('input.price').bind('keyup', function() {
    $('span.total').html( $('input.price').sumValues() );
});

to calculate a more complex formula of summations from user input.

The formula itself is (a through g are user inputs):

Step 1) X=((1+(a/100))^b)*(c*12)

Step 2) X+d=e

Step 3) e-f-g=h

Can you help me implement this more effectively?


回答1:


var X = Math.pow(1+(a/100), b)*(c*12);

var e = X+d;

var h = e-f-g;

Math.pow(base, exponent) is used for exponential calculation.

To set above value to input for example

$('#mortgage').val(X);

$('#utilities').val(e);

and so on.



来源:https://stackoverflow.com/questions/10803360/including-exponents-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!