Skip to content

Instantly share code, notes, and snippets.

@moresheth
Last active December 11, 2015 08:29
Show Gist options
  • Save moresheth/4573857 to your computer and use it in GitHub Desktop.
Save moresheth/4573857 to your computer and use it in GitHub Desktop.
This is a simple function to add up any numbers fed into the arguments: add( 1, 2.2, '3', true, 'monkey');
function add() {
var total = 0,
max_decimal = 0;
for (var i=0,l=arguments.length;i<l;i++) {
var num = parseFloat( arguments[i] ),
arr = ( num + '' ).split('.'),
dec = ( arr[1] == undefined ? 0 : arr[1].length );
if ( !isNaN(num) ) {
if ( max_decimal < dec ) max_decimal = dec;
total += parseFloat( arguments[i] );
}
}
return total.toFixed( max_decimal ) - 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment