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