Skip to content

Commit 78374f7

Browse files
NickNasomhdawson
authored andcommitted
doc: number documentation
PR-URL: #356 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 779560f commit 78374f7

File tree

1 file changed

+139
-8
lines changed

1 file changed

+139
-8
lines changed

doc/number.md

Lines changed: 139 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,160 @@
11
# Number
22

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+
47
## Methods
58

69
### Constructor
710

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+
878
```cpp
9-
Napi::Number::New(Napi::Env env, double value);
79+
Napi::Number::FloatValue() const;
1080
```
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.
1387

1488
```cpp
15-
Napi::Number::Number();
89+
Napi::Number::DoubleValue() const;
1690
```
17-
returns a new empty Javascript Number
1891

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+
2099
- `int32_t`
21100
- `uint32_t`
22101
- `int64_t`
23102
- `float`
24103
- `double`
25104

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.
27158

28159
```cpp
29160
uint32_t operatorVal = Napi::Number::New(Env(), 10.0); // Number to unsigned 32 bit integer

0 commit comments

Comments
 (0)