JavaScript General Use Full Number Methods and Properties.
JavaScript Usefull Number Methods and Properties. Some usefull number methods of javascript using in the javascript programming.
Number system methods helpfull when developer develop a project which scale large.
TOSTRING
The toString() is a number system method, which is use for returns a number as a string.
Example:
const num = 123;
var result = num.toString();
console.log(result);
// output
123
var result1 = (100+23).toString();
console.log(result1);
//output
123
TOEXPONENTIAL
The toExponential() method use for returns a string, with a number rounded and written using exponential notation.
Example:
const num1 = 9.656;
var result2 = num.toExponential(2);
console.log(result2);
// Output
9.66e+0
TOFIXED
The toFixed() method returns a string, with the number written with a specified number of decimals.
Example:
// TOFIXED Method
const num2 = 9.656;
var result3 = num2.toFixed(0);
console.log(result3);
// Output
10
var result4 = num2.toFixed(2);
console.log(result4);
//output
9.66
TOPRECISION
The toPrecision() method returns a string, with a number written with a specified lengh.
Example:
// TOPRECISION Method
const num3 = 9.656;
var result5 = num3.toPrecision(2);
console.log(result5);
// output
9.7
var result6 = num3.toPrecision(4);
console.log(result6);
//output
9.656
VALUEOF
The valueOf() method returns a number as a number.
Example:
// VALUEOF Method
const num4 = 123;
var result7 = num4.valueOf(4);
console.log(result7);
//output
123
NUMBER
The number() method can be used to convert JavaScript variables to numbers.
Example:
// Number Method
console.log(Number(true));
console.log(Number(false));
console.log(Number(“10”));
console.log(Number(“10.23”));
console.log(Number(“10,23”));
//oputput
1
0
10
10.23
NaN
PARSEINT
The parseInt() method perses a string and return a whole number. Spaces are allowed. Only the first number is returned.
Example:
// ParseInt Method
console.log(parseInt(“10”));
console.log(parseInt(“10.33”));
console.log(parseInt(“10 20 30”));
console.log(parseInt(“10 years”));
console.log(parseInt(“years 10”));
// Output
10
10
10
10
NaN
PARSEFLOAT
The parseFloat() method parses a string and return a number. Spaces are allowed. Only the first number is returned.
Example:
// ParseFloat Method
console.log(parseFloat(“10”));
console.log(parseFloat(“10.33”));
console.log(parseFloat(“10 20 30”));
console.log(parseFloat(“10 years”));
console.log(parseFloat(“years 10”));
// Output
10
10.33
10
10
NaN
For More Tips and Trics visit on : https://www.alert4me.com/forum/2020/08/17/javascript-general-use-full-number-methods-and-properties/