Jquery, adding input box totals with seperate input boxes for pence and pound

I have this code which works fine for adding up input boxes.

$.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 );
    sum = Math.round(sum*Math.pow(10,2))/Math.pow(10,2);
});
return sum;
};

$(document).ready(function() {
$('input.money1').bind('keyup', function() {
    $('#totalsales').html( $('input.money1').sumValues() );
});

});

However I want to have seperate boxes for pound/pence (money1 for the pound boxes and money2 for pence). I have tried adding

$('input.money2').bind('keyup', function() {
    $('#totalsales2').html( $('input.money2').sumValues() );
});

But as you can tell this will add them up other 100, is there anyway I can make it add 1 to totalsales for every one hundred?

JSFiddle - http://jsfiddle.net/RZrTa/

Upvotes: 1

Views: 367

Answers (2)

Ankur
Ankur

Reputation: 791

You can refer the jsfiddle link

http://jsfiddle.net/ankur200188/RZrTa/13/

Upvotes: 1

Lee
Lee

Reputation: 10603

Would be better off with a jsfiddle as im not 100% sure what you mean. If can can set up an example it would help get your answer.

But if all you want to do is take the pence value and for every 100 pence add 1 to the pounds then something like

//-- get the pennies
var pence = $('input.money2').sumValues();

//--- get the pounds in those pennies
var addtopounds = Math.floor(pence/100);

//-- for each pound, remove 100 pennies
pence -= addtopounds*100;

//-- now pence is what you stick in the total pence box
//-- and then the addtopounds number needs to be added to 
//-- the value of the total pounds

Upvotes: 1

Related Questions