Numbers can be rounded in different ways - they can be rounded to given significant figures, decimal places, or simply converted to standard form. In this section we'll discover three number methods to aid in numerical rounding.
Starting with the first in the list, the method toPrecision() serves to round a number to given significant figures.
Just to give you an example, the number 0.358 when rounded to the nearest 2 significant figures converts to 0.36. Similarly 157, when converted to 1 sf. gives 200. Basic math!
The number of significant figures, you desire, goes into the method as an argument which in turn throws out the rounded number as a string value.
Consider the following example:
var num = 0.358; var num2 = num.toPrecison(2); // "0.36"
Below, we experiment with some other arguments to toPrecision(), as applied on different numbers:
var num = 8956 num.toPrecision(3); // "8960" num = 15.85 num.toPrecision(3); // "15.9" num = 0.123 num.toPrecision(1); // "0.1"
The fact that toPrecision() returns a string can sometimes trick developers while they are working with it. They might forget to convert back the result to a number, and use it as it is in calculations, ultimately leading to bugs!
Therefore whenever you are working with it, remember to convert its output to a number using the ways discussed in the previous chapter.
Unfortunately this is the case with the next two methods as well.
As a quick example, consider the number 3.754 - it has three digits after the decimal point and likewise has a decimal place length of 3. When rounded to two decimal places, it will become 3.75 (since 4 is less than 5).
The method toFixed() is made to work on these digits i.e digits appearing after the decimal point.
Following is an illustration:
var num = 3.754; var roundedNum = num.toFixed(2); // "3.75"
The same number 3.754 is rounded to 2 decimal places, to give the stringified number "3.75".
Below, we experiment with some other arguments to the method, as applied on num:
num.toFixed(0); // "4" num.toFixed(1); // "3.8" num.toFixed(3); // "3.754" num.toFixed(4); // "3.7540"
First we round the number 3.754 to 0 decimal places i.e to the nearest unit, yielding 4. Second we round the number to 1 decimal place yielding 3.8. Next we round it to 3 decimal places, returning the number 3.754, as it is. Lastly we round it to 4 decimal places to give the number 3.7540.
Trivially, numbers without decimal points will also be rounded to the given decimal precision as you can see below.
var num = 10; var num2 = num.toFixed(2); // "10.00"
The number 10 is converted to "10.00", with exactly 2 decimal digits.
The method toExponential() rounds a number to the specified length of decimal places together with an exponent value. It is usually used on large numbers to convert them to a short, manageable form.
var num = 1056.7; num.toExponential(2); // returns "1.06e+3" num = 945682; num.toExponential(1); // returns "9.5e+5" num = 1987; num.toExponential(0); // returns "2e+3"
Recall from maths: this method can be used to give numbers in standard form.
Converting numbers to different bases
Although it merely converts a number into a string when called with no arguments, if it's provided with an argument, it will perform conversion of the number into the given base n representation.
The argument has to be a number and is generally called the radix of the converted number. Radix is simply the base, we wish to convert a number into.
To illustrate this discussion, following we convert the decimal number 15 into all the three representations - binary, octal and hexadecimal:
var num = 15; // conversion to binary var binNum = num.toString(2); // "1111" // conversion to octal var octNum = num.toString(8); // "17" // conversion to hexadecimal var hexNum = num.toString(16); // "f"
These three conversions are by far the most common out there, but you can still experiment with other bases. See what each one returns!
And once again, note that this method also converts the number into a string; which is, in effect, its sole purpose. Therefore, before working with the converted number just make sure to parse it using parseInt() and the respective base of the number.
Convert the binary number 10110 into hexadecimal representation, and save the result in a variable hexNum.
The following code has been set up for you to start with:
var binNum = "10110"; var hexNum = // put the result here
First we'll need to parse binNum into a number and then convert this to a hexadecimal number.
To parse binNum we'll use parseInt(binNum, 2) and then to convert its output into hexadecimal, we'll use toString(16).
Combining both these functions, we have the following code:
var binNum = "10110"; var hexNum = parseInt(binNum, 2).toString(16); // "16"