Skip to content

Instantly share code, notes, and snippets.

@kerryChen95
Created March 21, 2013 08:53
Show Gist options
  • Save kerryChen95/5211637 to your computer and use it in GitHub Desktop.
Save kerryChen95/5211637 to your computer and use it in GitHub Desktop.
判断浮点数相等;分号引发的血案。

example 1

var sum = 0.1 + 0.2; // 0.30000000000000004

// way 1:
Number(sum.toFixed(5)) === 0.3; // true

// way 2:
(sum - 0.3) < 1e-5; // true

example 2

var sum = 0.1 + 0.2; // 0.30000000000000004

// way 1:
Number(sum.toFixed(5)) === 0.3

// way 2:
(sum - 0.3) < 1e-5

TypeError: number is not a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment