JavaScript Numbers
Reading
Numbers
JavaScript has one number
type. This stores both integers and floating point
numbers (numbers with digits after the decimal point)
let score = 42const total = 10.1const pi = 3.14159265
Basic Math
+
, -
, *
, /
+
and -
represent addition and subtraction.
*
and /
represent multiplication and division.
Example:
const firstNumber = 42const secondNumber = 4const result = firstNumber / secondNumber
In this case result
is 10.5
.
%
This is known as the REMAINDER operator. The result of
firstNumber % secondNumber
is equal to whatever the remainder would be if we
divided secondNumber
into firstNumber
.
Taking our above example:
const firstNumber = 42const secondNumber = 4const result = firstNumber / secondNumberconst remainder = firstNumber % secondNumber
In this case result
is 10.5
and remainder
is 2
. The remainder is 2
because as we divide 4 into 42
we can only do so 10
times, leaving 2
over.
CAUTION: Remainder with negative numbers!
const firstNumber = -42const secondNumber = 4const result = firstNumber / secondNumberconst remainder = firstNumber % secondNumber
In this case, result
is -10.5
and remainder
is -2
! NOTE, in some
languages the %
means a modulus
and will always be positive
Remainder/% is useful for things like determining if a number is even or odd.
const isThreeEven = 3 % 2 == 0 // Falseconst isEightEven = 8 % 2 == 0 // True
It is also good for ensuring that a value is bounded by some value, causing it to wrap around.
// We don't want index to be 5 or more (stops at 4), and if it does it should wrap around to 0let index = 0index = (index + 1) % 5 // 0 + 1 is 1 -- 1 % 5 = 1console.log(`index is ${index}`)index = (index + 1) % 5 // 1 + 1 is 2 -- 2 % 5 = 2console.log(`index is ${index}`)index = (index + 1) % 5 // 2 + 1 is 3 -- 3 % 5 = 3console.log(`index is ${index}`)index = (index + 1) % 5 // 3 + 1 is 4 -- 4 % 5 = 4console.log(`index is ${index}`)// Here is where the % comes into play, adding one to 4 gives 5, but we want this to "wrap around" back to 0.index = (index + 1) % 5 // 4 + 1 is 5 -- 5 % 5 = 0console.log(`index is ${index}`)index = (index + 1) % 5 // 0 + 1 is 1 -- 1 % 5 = 1console.log(`index is ${index}`)index = (index + 1) % 5 // 1 + 1 is 2 -- 2 % 5 = 2console.log(`index is ${index}`)
Let's see this in a loop:
for (let index = 0; index < 20; index++) {const wrappedValue = index % 5console.log(`Wrapped value is ${wrappedValue} since index is ${index}`)}
Wrapped value is 0 since index is 0Wrapped value is 1 since index is 1Wrapped value is 2 since index is 2Wrapped value is 3 since index is 3Wrapped value is 4 since index is 4Wrapped value is 0 since index is 5Wrapped value is 1 since index is 6Wrapped value is 2 since index is 7Wrapped value is 3 since index is 8Wrapped value is 4 since index is 9Wrapped value is 0 since index is 10Wrapped value is 1 since index is 11Wrapped value is 2 since index is 12Wrapped value is 3 since index is 13Wrapped value is 4 since index is 14Wrapped value is 0 since index is 15Wrapped value is 1 since index is 16Wrapped value is 2 since index is 17Wrapped value is 3 since index is 18Wrapped value is 4 since index is 19
Conversions
parseInt('42')
Attempts to convert the given string into an int
value. It does its best
effort to parse what it is given. If it can't figure out how to parse the
number we get back the special value NaN
which represents "Not A Number". This
isn't a string, but a special value, in the same way null
is a special value.
Example:
const answer = parseInt('42')// answer is 42const mostlyAnAnswer = parseInt('42 things')// mostlyAnAnswer is still 42const notAnAnswer = parseInt('I think the answer is 42')// notAnAnswer is the value NaN
Number('42')
Another way to convert a string to a number is with the Number
method.
Number
is more strict than parseInt
in that the string must be strictly
formatted as a number. Any illegal characters will result in a value of NaN
.
const answer = Number('42.5')// answer is 42.5const mostlyAnAnswer = Number('42 things')// mostlyAnAnswer is the value NaNconst notAnAnswer = Number('I think the answer is 42')// notAnAnswer is the value NaN
ToString
A variable storing a number can use the toString
method to create a string
representation.
Example
const value = -16325const valueAsString = value.toString()// valueAsString will be `-16325`
Rounding
JavaScript
offers a number of ways to round numbers. The primary rounding
methods are round
, ceiling
, and floor
.
floor
The floor
method, Math.floor
, accepts a number
and returns the number
without any of the digits after the decimal part. The technical description is
Returns the largest integral value less than or equal to the specified number.
const price = 12.34const priceFloored = Math.floor(price)// priceFloored will be 12
Ceiling
The ceiling
method, Math.ceil
, accepts a number
and returns the smallest
whole number that is greater than or equal to the number. For instance if we
asked for the ceiling
of 42
we would get 42
back since it 42
a whole
number that is equal to the number we gave it. However, if we supplied 42.01
,
the next smallest whole number would be 43
.
const wholePrice = 42const wholePriceCeiling = Math.ceil(wholePrice)// wholePriceCeiling will be 42const priceWithMore = 42.01const priceWithMoreCeiling = Math.ceil(priceWithMore)// priceWithMoreCeiling will be 43
Round
Rounds a value to the nearest whole number. Optionally we can specify the number of fractional digits.
const pi = 3.14159265const roundedPi = Math.round(pi)// roundedPi will be 3 since we are rounding down