bigarith.js
Version:
Do very large math to precision!
80 lines (60 loc) • 3.43 kB
Markdown
`truncate()` returns the integer part of a number by removing the fractional part. There is a method function and a static method function.
```javascript
ba.truncate();
```
```javascript
BigArith.truncate(n);
```
*none*
The number to truncate. This could be a string of digits, a number, or a BigArith object.
A BigArith object with its value equals to the integer part of the value of the BigArith object it is called on.
A BigArith object with its value equals to the integer part of parameter n.
There are two functions which could be used, the *method function*, and the *static method function*. The method function takes no parameter and returns a BigArith object with its value equals to the integer part of the value of the BigArith object it is called on.
The static method function takes one parameter (n) and is always used as `BigArith.truncate()`. It returns a BigArith object with its value equals to the integer part of parameter n.
Unlike ceil(), floor(), and round(), truncate() does not do any rounding on the number. It just removes the fractional part and returns the integer remaining.
> Any number parameter (that is not strings of digits or a BigArith) should be between the <code>Number.MIN_SAFE_INTEGER</code> and <code>Number.MAX_SAFE_INTEGER</code> limits.
### Examples
> In the server-side, always remember to add the line `var BigArith = require('bigarith.js');` however every other thing remains the same in both server-side and client-side code.
```javascript
var ba = new BigArith("-45.456");
ba = ba.truncate(); //BigArith object with value "-45"
ba = new BigArith("-45.5");
ba = ba.truncate(); //BigArith object with value "-45"
ba = new BigArith("0.4");
ba = ba.truncate(); //BigArith object with value "0"
ba = new BigArith("0.5");
ba = ba.truncate(); //BigArith object with value "0"
```
```javascript
var ba = BigArith.truncate("-45.456"); //BigArith object with value "-45"
ba = BigArith.truncate("-45.5"); //BigArith object with value "-45"
ba = BigArith.truncate("0.4"); //BigArith object with value "0"
ba = BigArith.truncate("0.5"); //BigArith object with value "0"
```
Since the method returns a BigArith object, [method chaining](method_chaining.html) is possible.
```javascript
var ba = new BigArith("-17031986");
ba = ba.divide("+17031986").add("24011985").multiply("456785564").subtract("2"); //BigArith object with value "10968327654198974"
```
More examples [here](https://github.com/osofem/bigarith.js/tree/master/examples/). Full documentation [here](https://github.com/osofem/bigarith.js/tree/master/documentation)
* [abs()](https://osofem.github.io/bigarith.js/documentation/abs.html)
* [ceil()](https://osofem.github.io/bigarith.js/documentation/ceil.html)
* [floor()](https://osofem.github.io/bigarith.js/documentation/floor.html)
* [negate()](https://osofem.github.io/bigarith.js/documentation/negate.html)
* [round()](https://osofem.github.io/bigarith.js/documentation/round.html)