@@ -25,8 +25,8 @@ and I couldn't find a lightweight library to do it.
25
25
26
26
#### In Node.js / Deno
27
27
28
- ``` bash
29
- npm install decimal
28
+ ``` sh
29
+ npm install decimal
30
30
```
31
31
32
32
then in your program
@@ -37,29 +37,68 @@ let Decimal = require('decimal');
37
37
38
38
# Examples
39
39
40
- ``` js
41
- >>> 1.1 + 2.2
42
- 3.3000000000000003
43
- ```
40
+ ### Addition
44
41
45
42
``` js
46
- >>> Decimal (' 1.1' ).add (' 2.2' ).toNumber ()
47
- 3.3
43
+ // Regular JavaScript
44
+ >>> 0.1 + 0.2
45
+ 0.30000000000000004
46
+
47
+ // Using Decimal.js
48
+ >>> Decimal (' 0.1' ).add (' 0.2' ).toString ()
49
+ ' 0.3'
50
+
51
+ // Static method
52
+ >>> Decimal .add (' 0.1' , ' 0.2' ).toString ()
53
+ ' 0.3'
48
54
```
49
55
56
+ ### Subtraction
57
+
50
58
``` js
51
- >>> 4.01 * 2.01
52
- 8.060099999999998
59
+ // Regular JavaScript
60
+ >>> 0.3 - 0.1
61
+ 0.19999999999999998
62
+
63
+ // Using Decimal.js
64
+ >>> Decimal (' 0.3' ).sub (' 0.1' ).toString ()
65
+ ' 0.2'
66
+
67
+ // Static method
68
+ >>> Decimal .sub (' 0.3' , ' 0.1' ).toString ()
69
+ ' 0.2'
53
70
```
54
71
72
+ ### Multiplication
73
+
55
74
``` js
56
- >>> Decimal (' 4.01' ).mul (' 2.01' ).toNumber ()
57
- 8.0601
75
+ // Regular JavaScript
76
+ >>> 4.01 * 2.01
77
+ 8.060099999999998
78
+
79
+ // Using Decimal.js
80
+ >>> Decimal (' 4.01' ).mul (' 2.01' ).toString ()
81
+ ' 8.0601'
82
+
83
+ // Static method
84
+ >>> Decimal .mul (' 4.01' , ' 2.01' ).toString ()
85
+ ' 8.0601'
58
86
```
59
87
88
+ ### Division
89
+
60
90
``` js
61
- >>> Decimal .mul (' 4.01' , ' 2.01' ).toNumber ()
62
- 8.0601
91
+ // Regular JavaScript
92
+ >>> 1.21 / 0.1
93
+ 12.100000000000001
94
+
95
+ // Using Decimal.js
96
+ >>> Decimal (' 1.21' ).div (' 0.1' ).toString ()
97
+ ' 12.1'
98
+
99
+ // Static method
100
+ >>> Decimal .div (' 1.21' , ' 0.1' ).toString ()
101
+ ' 12.1'
63
102
```
64
103
65
104
## Can I help?
@@ -81,26 +120,56 @@ another `Decimal`.
81
120
82
121
Returns the ` Decimal ` instance as a string.
83
122
123
+ ``` js
124
+ >>> Decimal (' 123.456' ).toString ()
125
+ ' 123.456'
126
+ ```
127
+
84
128
#### .toNumber()
85
129
86
130
Turn a ` Decimal ` into a ` Number ` .
87
131
132
+ ``` js
133
+ >>> Decimal (' 123.456' ).toNumber ()
134
+ 123.456
135
+ ```
136
+
88
137
#### .add(n)
89
138
90
139
Return a new ` Decimal ` containing the instance value plus ` n ` .
91
140
141
+ ``` js
142
+ >>> Decimal (' 0.1' ).add (' 0.2' ).toString ()
143
+ ' 0.3'
144
+ ```
145
+
92
146
#### .sub(n)
93
147
94
148
Return a new ` Decimal ` containing the instance value minus ` n ` .
95
149
150
+ ``` js
151
+ >>> Decimal (' 0.3' ).sub (' 0.1' ).toString ()
152
+ ' 0.2'
153
+ ```
154
+
96
155
#### .mul(n)
97
156
98
157
Return a new ` Decimal ` containing the instance value multiplied by ` n ` .
99
158
159
+ ``` js
160
+ >>> Decimal (' 4.01' ).mul (' 2.01' ).toString ()
161
+ ' 8.0601'
162
+ ```
163
+
100
164
#### .div(n)
101
165
102
166
Return a new ` Decimal ` containing the instance value integrally divided by ` n ` .
103
167
168
+ ``` js
169
+ >>> Decimal (' 1.21' ).div (' 0.1' ).toString ()
170
+ ' 12.1'
171
+ ```
172
+
104
173
### Numeric Operations
105
174
106
175
#### .abs()
0 commit comments