자바스크립트로 소수점이하의 값을 가진 수를 사칙계산하다 보면 가끔 이상한 값이 나온다. 0.1+0.2 = 0.3이 되어야 하는데 계산해보면 소수점 아래로 값이 더 붙어 있다.
let sum = 0.1 + 0.2
console.log(sum)
// result : 0.30000000000000004
+
를 붙여준다let a = 0.1
let b = 0.2
let c = a + b
console.log("c : " + c)
let d = c.toFixed();
console.log("typeof d : " + typeof d)
let e = +d
console.log("typeof e : " + typeof e)
결과
c : 0.30000000000000004
typeof d : string
typeof e : number