1# Number
2
3`Napi::Number` class is a representation of the JavaScript `Number` object. The
4`Napi::Number` class inherits its behavior from `Napi::Value` class
5(for more info see [`Napi::Value`](value.md))
6
7## Methods
8
9### Constructor
10
11Creates a new _empty_ instance of a `Napi::Number` object.
12
13```cpp
14Napi::Number();
15```
16
17Returns a new _empty_ `Napi::Number` object.
18
19### Constructor
20
21Creates a new instance of a `Napi::Number` object.
22
23```cpp
24Napi::Number(napi_env env, napi_value value);
25```
26
27 - `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
28 - `[in] value`: The JavaScript value holding a number.
29
30 Returns a non-empty `Napi::Number` object.
31
32 ### New
33
34 Creates a new instance of a `Napi::Number` object.
35
36```cpp
37Napi::Number Napi::Number::New(napi_env env, double value);
38```
39 - `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
40 - `[in] value`: The C++ primitive from which to instantiate the `Napi::Number`.
41
42Creates a new instance of a `Napi::Number` object.
43
44### Int32Value
45
46Converts a `Napi::Number` value to a `int32_t` primitive type.
47
48```cpp
49Napi::Number::Int32Value() const;
50```
51
52Returns the `int32_t` primitive type of the corresponding `Napi::Number` object.
53
54### Uint32Value
55
56Converts a `Napi::Number` value to a `uint32_t` primitive type.
57
58```cpp
59Napi::Number::Uint32Value() const;
60```
61
62Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object.
63
64### Int64Value
65
66Converts a `Napi::Number` value to a `int64_t` primitive type.
67
68```cpp
69Napi::Number::Int64Value() const;
70```
71
72Returns the `int64_t` primitive type of the corresponding `Napi::Number` object.
73
74### FloatValue
75
76Converts a `Napi::Number` value to a `float` primitive type.
77
78```cpp
79Napi::Number::FloatValue() const;
80```
81
82Returns the `float` primitive type of the corresponding `Napi::Number` object.
83
84### DoubleValue
85
86Converts a `Napi::Number` value to a `double` primitive type.
87
88```cpp
89Napi::Number::DoubleValue() const;
90```
91
92Returns the `double` primitive type of the corresponding `Napi::Number` object.
93
94## Operators
95
96The `Napi::Number` class contains a set of operators to easily cast JavaScript
97`Number` object to one of the following primitive types:
98
99 - `int32_t`
100 - `uint32_t`
101 - `int64_t`
102 - `float`
103 - `double`
104
105### operator int32_t
106
107Converts a `Napi::Number` value to a `int32_t` primitive.
108
109```cpp
110Napi::Number::operator int32_t() const;
111```
112
113Returns the `int32_t` primitive type of the corresponding `Napi::Number` object.
114
115### operator uint32_t
116
117Converts a `Napi::Number` value to a `uint32_t` primitive type.
118
119```cpp
120Napi::Number::operator uint32_t() const;
121```
122
123Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object.
124
125### operator int64_t
126
127Converts a `Napi::Number` value to a `int64_t` primitive type.
128
129```cpp
130Napi::Number::operator int64_t() const;
131```
132
133Returns the `int64_t` primitive type of the corresponding `Napi::Number` object.
134
135### operator float
136
137Converts a `Napi::Number` value to a `float` primitive type.
138
139```cpp
140Napi::Number::operator float() const;
141```
142
143Returns the `float` primitive type of the corresponding `Napi::Number` object.
144
145### operator double
146
147Converts a `Napi::Number` value to a `double` primitive type.
148
149```cpp
150Napi::Number::operator double() const;
151```
152
153Returns the `double` primitive type of the corresponding `Napi::Number` object.
154
155### Example
156
157The following shows an example of casting a number to an `uint32_t` value.
158
159```cpp
160uint32_t operatorVal = Napi::Number::New(Env(), 10.0); // Number to unsigned 32 bit integer
161// or
162auto instanceVal = info[0].As<Napi::Number>().Uint32Value();
163```
164