Number Object

The Number object contains several properties and methods. We can use the properties and methods without declaring the Number object.



   Property Explanation
MAX_VALUE largest positive number in JavaScript
MIN_VALUE smallest positive number in JavaScript
NaN means not a number
NEGATIVE_INFINITY represent the value of negative infinity
POSITIVE_INFINITY represent the value of positive infinity




Method Explanation
Number() Object constructor
toString() converts numbers to string - you can use a radix, which is optional, inside the parentheses
toExponential(decimals) converts numbers to a string in exponential notation
toFixed(decimals) converts numbers to a string using fixed point notation
toPrecision(decimals)() converts a number to a string - rounded to the specified number of significant digits




Examples


toString()

<script>
var ticket=6;
var people=10;
var total cost=ticket*people;
document.write("Total £" + total.toString() );
</script>

Displays






toString(radix)

<script>
var number=32;
//converting 32 decimal to binary and hex;
document.write ("32 decimal to: <br>")
documentWrite("binary:"+ number.toString(2));
document.write ("<br>")
document.write ("hex:" + number.toString(16));
</script>

Displays






toFixed(decimals)

<script>
var value = 456.898745;
//after the decimal point only 3 numbers are fixed
document.write(value.toFixed(3));
</script>

Displays







toPrecision(decimals)

<script>
var value = 456.898745
//rounded to 5 significant digit;
document.write( value.toPrecision(5));
</script>

Displays






toExponential(decimals)

<script>
var value = 456.898745;
//decimal point moves 2 places to the left;
document.write( value.toExponential(2));
</script>

Displays




Now do the Exercise!