Numbers and mathematics come into most scripts at some point, be it in the form of the simple arithmetic required to add up prices and work out sales tax, the process of generating and using random numbers, or the more complex mathematics involved animation.
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
Math.sqrt : returns the square root of a number do Math.sqrt(9) returns 3
Math .random: returns a pseudorandom number between zero and one
Rounding a Number to x Decimal Places
In some applications, you may need to round the result to a certain number of decimal places. For instance, you may want to display temperature values in 0.1-degree increments, even you though your script has access to more precise values.
So the question is how to do it?
We can write a function that will round to any number of places, as follows:
So, for example, if we started with a number like n = 3.492867, we could call roundTo(n, 0) to get 3 or roundTo(n, 2) to get 3.49
Creating and Constraining Random Numbers
Random numbers can be used as the basis for any task that needs to have a random or semi-random aspect.
We already know how to generate a random number between 0 and 1 by using Math.random() function but what if we need random numbers between 0-100 or maybe 8-23 or 23-1995 etc.
Well there is a solution
function to generate random between any two number.
so, for example we could generate a number between 0 and 100 by calling randomBetween(0, 100), or a number between 8 and 1995 by calling radomBetween(8, 1995).
using randomBetween() function to get random numbers.
Converting a Number to a String
Once you’ve finished a calculation, you might want to turn the output into something more readable, such as formatting a value to represent currency.To do this we must convert the number to a string.
The most direct means of converting a number to a string is to built -in String constructor function:
converting number to string using built-in String constructor
converting string using toString() method
Another useful technique is string concatenation (joining strings together), which returns a string even if some of the input values are numbers:
Converting a String to a Number
Like Number to String, conversion of String to a Number is also possible.
The most direct means of converting a string to a number is to use the built-in Number constructor function:
conversion using a built-in Number constructor function
Another handy technique is to use the parseInt and ParseFloat functions, which will attempt to find and return an integer or decimal number at the start of a string.
conversion using parseInt and parseFloat
note : If the first character of the input is not a digit or some other numerical character , these functions will not be ale to return a number. In such cases. they’ll return the special value NaN (Not a Number).