# 【Web前端问题】计算器实现0.1+0.2==0.3

### 回答：

``````function max(a, b) {
a = a | 0;
b = b | 0;
return a > b ? a | 0 : b | 0;
}

class BigDecimal {
constructor(arr) {
if (typeof arr == 'string') return BigDecimal.of(arr);

this.arr = arr;
this.i = this.arr.indexOf('.');
if (this.i == -1) this.i = arr.length;
}

static of(str) {
var arr = str.split('');
for (var i = 0; i < arr.length; ++i) if (arr[i] != '.') arr[i] = arr[i] | 0;
return new BigDecimal(arr);
}

index(i) {
i = i | 0;
if (i >= 0) {
return this.i - 1 - i | 0;
} else {
return this.i - i | 0;
}
}

at(i) {
return this.arr[this.index(i)] | 0;
}

int() {
return this.i;
}

dec() {
return this.arr.length - this.i - 1;
}

add(that) {
var int = max(this.int(), that.int()) | 0;
var dec = max(this.dec(), that.dec()) | 0;
var ret = new Array(1 + int + 1 + dec).fill(0);
ret[1 + int] = '.';
ret = new BigDecimal(ret);

var c = 0;
for (var i = dec; i > 0; --i) {
var r = this.at(-i) + that.at(-i) + c;
c = r / 10 | 0;
r = r % 10 | 0;

ret.arr[ret.index(-i)] = r;
}

for (var i = 0; i < int; ++i) {
var r = this.at(i) + that.at(i) + c;
c = r / 10 | 0;
r = r % 10 | 0;

ret.arr[ret.index(i)] = r;
}
ret.arr[ret.index(int)] = c;

return ret;
}

toString() {
var int = this.int();
for (var a = 0; a < int - 1; ++a) {
if ((this.arr[a] | 0) != 0) break;
}

return this.arr.slice(a).join('');
}
}

var a = new BigDecimal('0.1');
var b = new BigDecimal('0.2');

var c = a.add(b);

console.log(c + ''); // 0.3``````

math.js

### 回答：

1. 按小数点分割，然后各自计算，再拼在一起。。
2. 小数位固定且数字不很大的话，可以转换成整数计算后再转换回去
3. 自己写算法，不难，四则运算竖式算法就够了，还可以支持无限大数计算