File: number.md

package info (click to toggle)
node-addon-api 8.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: cpp: 15,431; javascript: 5,631; ansic: 157; makefile: 7
file content (163 lines) | stat: -rw-r--r-- 3,648 bytes parent folder | download | duplicates (3)
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
152
153
154
155
156
157
158
159
160
161
162
163
# Number

`Napi::Number` class is a representation of the JavaScript `Number` object. The
`Napi::Number` class inherits its behavior from `Napi::Value` class
(for more info see [`Napi::Value`](value.md))

## Methods

### Constructor

Creates a new _empty_ instance of a `Napi::Number` object.

```cpp
Napi::Number();
```

Returns a new _empty_ `Napi::Number` object.

### Constructor

Creates a new instance of a `Napi::Number` object.

```cpp
Napi::Number(napi_env env, napi_value value);
```

 - `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
 - `[in] value`: The JavaScript value holding a number.

 Returns a non-empty `Napi::Number` object.

 ### New

 Creates a new instance of a `Napi::Number` object.

```cpp
Napi::Number Napi::Number::New(napi_env env, double value);
```
 - `[in] env`: The `napi_env` environment in which to construct the `Napi::Number` object.
 - `[in] value`: The C++ primitive from which to instantiate the `Napi::Number`.

Creates a new instance of a `Napi::Number` object.

### Int32Value

Converts a `Napi::Number` value to a `int32_t` primitive type.

```cpp
Napi::Number::Int32Value() const;
```

Returns the `int32_t` primitive type of the corresponding `Napi::Number` object.

### Uint32Value

Converts a `Napi::Number` value to a `uint32_t` primitive type.

```cpp
Napi::Number::Uint32Value() const;
```

Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object.

### Int64Value

Converts a `Napi::Number` value to a `int64_t` primitive type.

```cpp
Napi::Number::Int64Value() const;
```

Returns the `int64_t` primitive type of the corresponding `Napi::Number` object.

### FloatValue

Converts a `Napi::Number` value to a `float` primitive type.

```cpp
Napi::Number::FloatValue() const;
```

Returns the `float` primitive type of the corresponding `Napi::Number` object.

### DoubleValue

Converts a `Napi::Number` value to a `double` primitive type.

```cpp
Napi::Number::DoubleValue() const;
```

Returns the `double` primitive type of the corresponding `Napi::Number` object.

## Operators

The `Napi::Number` class contains a set of operators to easily cast JavaScript
`Number` object to one of the following primitive types:

 - `int32_t`
 - `uint32_t`
 - `int64_t`
 - `float`
 - `double`

### operator int32_t

Converts a `Napi::Number` value to a `int32_t` primitive.

```cpp
Napi::Number::operator int32_t() const;
```

Returns the `int32_t` primitive type of the corresponding `Napi::Number` object.

### operator uint32_t

Converts a `Napi::Number` value to a `uint32_t` primitive type.

```cpp
Napi::Number::operator uint32_t() const;
```

Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object.

### operator int64_t

Converts a `Napi::Number` value to a `int64_t` primitive type.

```cpp
Napi::Number::operator int64_t() const;
```

Returns the `int64_t` primitive type of the corresponding `Napi::Number` object.

### operator float

Converts a `Napi::Number` value to a `float` primitive type.

```cpp
Napi::Number::operator float() const;
```

Returns the `float` primitive type of the corresponding `Napi::Number` object.

### operator double

Converts a `Napi::Number` value to a `double` primitive type.

```cpp
Napi::Number::operator double() const;
```

Returns the `double` primitive type of the corresponding `Napi::Number` object.

### Example

The following shows an example of casting a number to an `uint32_t` value.

```cpp
uint32_t operatorVal = Napi::Number::New(Env(), 10.0); // Number to unsigned 32 bit integer
// or
auto instanceVal = info[0].As<Napi::Number>().Uint32Value();
```