function roundFixed(num, fixed) {
var pos = num.toString().indexOf('.'),
decimal_places = num.toString().length - pos - 1,
_int = num * Math.pow(10, decimal_places),
divisor_1 = Math.pow(10, decimal_places - fixed),
divisor_2 = Math.pow(10, fixed);
return Math.round(_int / divisor_1) / divisor_2;
} console.log(roundFixed(3.135,2));//-> 3.14
console.log(roundFixed(2.135,2));//-> 2.14
console.log(roundFixed(0.955,1));//-> 1
console.log(roundFixed(0.955,2));//-> 0.96
console.log(roundFixed(1.955,2));//-> 1.96
console.log(roundFixed(1.955,1));//-> 2
参考:http://bbs.****.net/topics/391957876 perhapschen的回答