Dealing with numbers in javascript
Numbers in JavaScript the Basics
- JavaScript has just one number type. i.e
5and5.10are the same type. - JS uses the “double-precision 64-bit format IEEE 754” standard
What does this mean? All JavaScript numbers are stored as double floating point numbers. JS will trick you into thinking that your const x = 5 is an integer, but really it’s a float and equal to x = 5.0
test youself !!!
5 == 5.0
true
Math Object
JavaScript has a built-in Math object that provides a range of mathematical methods and properties. The most useful methods are:
Math.ceil : rounds a number upwards, so Math.ceil(8.23) returns 9
Math.floor : rounds a number downwards, so Math.floor(23.8) returns 23
Math.round : rounds the number to the nearest integer, so
Math.round(8.3) returns 8, while Math.round(2.8) returns 3
Math.pow : raises one number to the power of another so Math.pow(2,3) returns
Testyourself:
Math.ceil(28.8)
29
Math.floor(28.8)
28
Math.floor(28.8)
28
Math.round(25.7)
26
Comments
Post a Comment