方法一:使用github上的庫:BigDecimal.js或bignumber.jsgit
方法二:使用簡單點四捨五入方法,其實跟上面的方法差很少,只不過取了一個10位小數github
function numTofixed(num) { if (typeof num == 'number') { num = parseFloat(num.toFixed(10)) } return num; } numTofixed(0.1 + 0.2);
方法三:spa
function fixFloatCalcRudely(num){ if(typeof num == 'number'){ var str=num.toString(), match=str.match(/\.(\d*?)(9|0)\2{5,}(\d{1,5})$/); if(match != null){ return num.toFixed(match[1].length)-0; } } return num; }