function decimalAdjust(type, value, exp) {
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
Number.prototype.toFixedB = function toFixed ( precision ) {
var multiplier = Math.pow( 10, precision + 1 ),
wholeNumber = Math.floor( this * multiplier );
return (Math.round( wholeNumber / 10 ) * 10 / multiplier).toFixed(precision);
Number.prototype.toFixed10 = function(precision) {
return Math.round10(this, -precision).toFixed(precision);
$('#in').change(function() {
$('#toFixed > span').text(parseFloat($(this).val()).toFixed(2));
$('#toFixedB > span').text(parseFloat($(this).val()).toFixedB(2));
$('#toFixed10 > span').text(parseFloat($(this).val()).toFixed10(2));