-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16 number methods.js
More file actions
155 lines (115 loc) · 4.61 KB
/
16 number methods.js
File metadata and controls
155 lines (115 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
************JavaScript Number Methods*******************
Number methods help you work with numbers.
Number Methods and Properties
Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects).
But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
********************The toString() Method********************
The toString() method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions):
*/
var s, x = 123;
s=x.toString(); // returns 123 from variable x
console.log(s);
s=(123).toString(); // returns 123 from literal 123
console.log(s);
s=(100 + 23).toString(); // returns 123 from expression 100 + 23
console.log(s);
console.log(typeof(s));
/*
************The toExponential() Method*****************
toExponential() returns a string, with a number rounded and written using exponential notation.
A parameter defines the number of characters behind the decimal point:
*/
x=9.656;
s=x.toExponential(2); // returns 9.66e+0
console.log(typeof(s));
s=x.toExponential(4); // returns 9.6560e+0
console.log(typeof(s));
s=x.toExponential(6); // returns 9.656000e+0
/*
*********************The toFixed() Method***************************
toFixed() returns a string, with the number written with a specified number of decimals:
*/
x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
/*
******************The toPrecision() Method********************
toPrecision() returns a string, with a number written with a specified length:
*/
x = 9.656;
x.toPrecision(); // returns 9.656
x.toPrecision(2); // returns 9.7
x.toPrecision(4); // returns 9.656
x.toPrecision(6); // returns 9.65600
/*
******************The valueOf() Method*******************
valueOf() returns a number as a number.
*/
x = 123;
x.valueOf(); // returns 123 from variable x
(123).valueOf(); // returns 123 from literal 123
(100 + 23).valueOf(); // returns 123 from expression 100 + 23
x=new Number(30);
console.log(typeof(x));//object
x=x.valueOf();
console.log(typeof(x));//number
/*
In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object).
The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.
There is no reason to use it in your code.
All JavaScript data types have a valueOf() and a toString() method.
*/
/*
****************Converting Variables to Numbers****************
There are 3 JavaScript methods that can be used to convert variables to numbers:
The Number() method
The parseInt() method
The parseFloat() method
These methods are not number methods, but global JavaScript methods.
*/
/*
Global JavaScript Methods
JavaScript global methods can be used on all JavaScript data types.
These are the most relevant methods, when working with numbers:
Method Description
Number() Returns a number, converted from its argument.
parseFloat() Parses its argument and returns a floating point number
parseInt() Parses its argument and returns an integer
*/
Number(true); // returns 1
Number(false); // returns 0
Number("10"); // returns 10
Number(" 10"); // returns 10
Number("10 "); // returns 10
Number(" 10 "); // returns 10
Number("10.33"); // returns 10.33
Number("10,33"); // returns NaN
Number("10 33"); // returns NaN
Number("John"); // returns NaN
// If the number cannot be converted, NaN (Not a Number) is returned.
/*
The parseInt() Method
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
*/
parseInt("-10"); // returns -10
parseInt("-10.33"); // returns -10
parseInt("10"); // returns 10
parseInt("10.33"); // returns 10
parseInt("10 20 30"); // returns 10
parseInt("10 years"); // returns 10
parseInt("years 10"); // returns NaN
//If the number cannot be converted, NaN (Not a Number) is returned.
/*
The parseFloat() Method
parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:
*/
parseFloat("10"); // returns 10
parseFloat("10.33"); // returns 10.33
parseFloat("10 20 30"); // returns 10
parseFloat("10 years"); // returns 10
parseFloat("years 10"); // returns NaN
//If the number cannot be converted, NaN (Not a Number) is returned.