Skip to content

Commit 0db42c1

Browse files
authored
chore: Added examples for every operations (#19)
1 parent 8f53839 commit 0db42c1

File tree

1 file changed

+83
-14
lines changed

1 file changed

+83
-14
lines changed

README.md

+83-14
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ and I couldn't find a lightweight library to do it.
2525

2626
#### In Node.js / Deno
2727

28-
```bash
29-
npm install decimal
28+
```sh
29+
npm install decimal
3030
```
3131

3232
then in your program
@@ -37,29 +37,68 @@ let Decimal = require('decimal');
3737

3838
# Examples
3939

40-
```js
41-
>>> 1.1 + 2.2
42-
3.3000000000000003
43-
```
40+
### Addition
4441

4542
```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'
4854
```
4955

56+
### Subtraction
57+
5058
```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'
5370
```
5471

72+
### Multiplication
73+
5574
```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'
5886
```
5987

88+
### Division
89+
6090
```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'
63102
```
64103

65104
## Can I help?
@@ -81,26 +120,56 @@ another `Decimal`.
81120

82121
Returns the `Decimal` instance as a string.
83122

123+
```js
124+
>>> Decimal('123.456').toString()
125+
'123.456'
126+
```
127+
84128
#### .toNumber()
85129

86130
Turn a `Decimal` into a `Number`.
87131

132+
```js
133+
>>> Decimal('123.456').toNumber()
134+
123.456
135+
```
136+
88137
#### .add(n)
89138

90139
Return a new `Decimal` containing the instance value plus `n`.
91140

141+
```js
142+
>>> Decimal('0.1').add('0.2').toString()
143+
'0.3'
144+
```
145+
92146
#### .sub(n)
93147

94148
Return a new `Decimal` containing the instance value minus `n`.
95149

150+
```js
151+
>>> Decimal('0.3').sub('0.1').toString()
152+
'0.2'
153+
```
154+
96155
#### .mul(n)
97156

98157
Return a new `Decimal` containing the instance value multiplied by `n`.
99158

159+
```js
160+
>>> Decimal('4.01').mul('2.01').toString()
161+
'8.0601'
162+
```
163+
100164
#### .div(n)
101165

102166
Return a new `Decimal` containing the instance value integrally divided by `n`.
103167

168+
```js
169+
>>> Decimal('1.21').div('0.1').toString()
170+
'12.1'
171+
```
172+
104173
### Numeric Operations
105174

106175
#### .abs()

0 commit comments

Comments
 (0)