Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
royNiladri committed Jan 20, 2024
2 parents a899bab + 42950f1 commit 41a40de
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 152 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2021 Niladri Roy
Copyright (c) 2017-2024 Niladri Roy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -30,6 +30,7 @@
- [setValue()](#setvalue)
- [getPrettyValue(number, digits, separator)](#getprettyvaluenumber-digits-separator)
- [round(number, precision, roundingMode)](#roundnumber-precision-roundingmode)
- [stripTrailingZero(number)](#striptrailingzeronumber)
- [abs(number)](#absnumber)
- [floor(number)](#floornumber)
- [ceil(number)](#ceilnumber)
Expand Down Expand Up @@ -154,6 +155,19 @@ var numRound1 = num.round(1, bigDecimal.RoundingModes.DOWN); // "123.6"
var numRound2 = num.round(2, bigDecimal.RoundingModes.CEILING); // "123.66"
```

### stripTrailingZero(number)
Returns the number with trailing zeroes (prefix and suffix) removed.
```javascript
var n1 = bigDecimal.stripTrailingZero(300.30) // "300.3"
var n2 = bigDecimal.stripTrailingZero(-0015.1) // "-15.1"
var n3 = bigDecimal.stripTrailingZero(0.000) // by default defined as "0"
```
The instance returns the result as new `bigDecimal`
```javascript
var n1 = new bigDecimal(5.100).stripTrailingZero() // bigDecimal(5.1)
var n2 = new bigDecimal(1.05).add(new bigDecimal(1.05)).stripTrailingZero() // bigDecimal(2.1)
```

### abs(number)
Returns the absolute value of a number.
```javascript
Expand Down
321 changes: 197 additions & 124 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/big-decimal.spec.ts
Expand Up @@ -469,4 +469,15 @@ describe("BIG-DECIMAL", function () {
).toBe("24.0");
});
});

describe("stripTrailingZero", function () {
it("should: static function running", function () {
expect(bigDecimal.stripTrailingZero("44")).toBe("44");
});

it("should: class function running", function () {
expect(new bigDecimal("00.001200").stripTrailingZero().getValue()).toBe("0.0012");
});

});
});
10 changes: 10 additions & 0 deletions src/big-decimal.ts
Expand Up @@ -7,6 +7,7 @@ import { modulus } from "./modulus";
import { compareTo } from "./compareTo";
import { subtract, negate } from "./subtract";
import { RoundingModes as Modes, RoundingModes } from "./roundingModes";
import { stripTrailingZero } from "./stripTrailingZero";

class bigDecimal {
private value: string;
Expand Down Expand Up @@ -214,5 +215,14 @@ class bigDecimal {
negate() {
return new bigDecimal(negate(this.value));
}

static stripTrailingZero(number) {
number = bigDecimal.validate(number);
return stripTrailingZero(number);
}

stripTrailingZero() {
return new bigDecimal(stripTrailingZero(this.value));
}
}
export default bigDecimal;
34 changes: 7 additions & 27 deletions src/multiply.ts
@@ -1,3 +1,5 @@
import { stripTrailingZero } from "./stripTrailingZero"

export function multiply(number1, number2) {
number1 = number1.toString();
number2 = number2.toString();
Expand All @@ -12,8 +14,8 @@ export function multiply(number1, number2) {
negative++;
number2 = number2.substr(1);
}
number1 = trailZero(number1);
number2 = trailZero(number2);
number1 = stripTrailingZero(number1);
number2 = stripTrailingZero(number2);
let decimalLength1 = 0;
let decimalLength2 = 0;

Expand All @@ -25,8 +27,8 @@ export function multiply(number1, number2) {
decimalLength2 = number2.length - number2.indexOf('.') - 1;
}
let decimalLength = decimalLength1 + decimalLength2;
number1 = trailZero(number1.replace('.', ''));
number2 = trailZero(number2.replace('.', ''));
number1 = stripTrailingZero(number1.replace('.', ''));
number2 = stripTrailingZero(number2.replace('.', ''));

if (number1.length < number2.length) {
let temp = number1;
Expand Down Expand Up @@ -65,7 +67,7 @@ export function multiply(number1, number2) {
/*
* Formatting result
*/
result = trailZero(adjustDecimal(result, decimalLength));
result = stripTrailingZero(adjustDecimal(result, decimalLength));
if (negative == 1) {
result = '-' + result;
}
Expand All @@ -83,25 +85,3 @@ function adjustDecimal(number, decimal) {
return number.substr(0, number.length - decimal) + '.' + number.substr(number.length - decimal, decimal)
}
}

/*
* Removes zero from front and back*/
function trailZero(number) {
while (number[0] == '0') {
number = number.substr(1);
}
if (number.indexOf('.') != -1) {
while (number[number.length - 1] == '0') {
number = number.substr(0, number.length - 1);
}
}
if (number == "" || number == ".") {
number = '0';
} else if (number[number.length - 1] == '.') {
number = number.substr(0, number.length - 1);
}
if (number[0] == '.') {
number = '0' + number;
}
return number;
}
89 changes: 89 additions & 0 deletions src/stripTrailingZero.spec.ts
@@ -0,0 +1,89 @@
import { stripTrailingZero } from "./stripTrailingZero";
import { roundOff } from "./round";
import bigDecimal from "./big-decimal";

describe("stripTrailingZero", function () {
it("should be defined", function () {
expect(stripTrailingZero).toBeDefined();
});

it(`should: 'foo' still becomes 'foo' since it's not valid decimal`, function () {
expect(stripTrailingZero("foo")).toBe("foo");
});

it(`should: '' (empty string) becomes 0`, function () {
expect(stripTrailingZero("")).toBe("0");
});

it(`should: '.' (dot string) becomes 0`, function () {
expect(stripTrailingZero(".")).toBe("0");
});

it("should: 44 becomes 44", function () {
expect(stripTrailingZero("44")).toBe("44");
});

it("should: 00.001200 becomes 0.0012", function () {
expect(stripTrailingZero("00.001200")).toBe("0.0012");
});

it("should: 035.02 becomes 35.02", function () {
expect(stripTrailingZero("035.02")).toBe("35.02");
});

it("should: -50.70 becomes -50.7", function () {
expect(stripTrailingZero("-50.70")).toBe("-50.7");
});

it("should: 0.000 becomes 0", function () {
expect(stripTrailingZero("0.000")).toBe("0");
});

it("should: 000.0 becomes 0", function () {
expect(stripTrailingZero("000.0")).toBe("0");
});

it("should: 00000000000000000001.1 becomes 1.1", function () {
expect(stripTrailingZero("00000000000000000001.1")).toBe("1.1");
});

it("should: 1.10000000000000000 becomes 1.1", function () {
expect(stripTrailingZero("1.10000000000000000")).toBe("1.1");
});

it("should: -00000000000000000005.5 becomes -5.5", function () {
expect(stripTrailingZero("-00000000000000000005.5")).toBe("-5.5");
});

it("should: -5.50000000000000000 becomes -5.5", function () {
expect(stripTrailingZero("-5.50000000000000000")).toBe("-5.5");
});

// Usage in conjugation with rounding
it("should: result of remove trailing zeroes then rounding 1.550 to 1 digit precision becomes 1.6", function () {
expect((new bigDecimal("1.550")).stripTrailingZero().round(1).getValue()).toBe("1.6");
});
it("should: result of rounding 1.550 to 1 digit precision then remove trailing zeroes becomes 1.6", function () {
expect((new bigDecimal("1.550")).round(1).stripTrailingZero().getValue()).toBe("1.6");
});
it("should: result of remove trailing zeroes then rounding -1.550 to 1 digit precision becomes -1.6", function () {
expect((new bigDecimal("-1.550")).stripTrailingZero().round(1).getValue()).toBe("-1.6");
});
it("should: result of rounding -1.550 to 1 digit precision then remove trailing zeroes becomes -1.6", function () {
expect((new bigDecimal("-1.550")).round(1).stripTrailingZero().getValue()).toBe("-1.6");
});

// Usage in conjugation with operators
it("should: 1.50 + 1.50 stripped to 3", function () {
expect((new bigDecimal("1.50")).add(new bigDecimal("1.50")).stripTrailingZero().getValue()).toBe("3");
});
it("should: 4.505 - 0.005 stripped to 4.5", function () {
expect((new bigDecimal("4.505")).subtract(new bigDecimal("0.005")).stripTrailingZero().getValue()).toBe("4.5");
});
it("should: 1.505 * 2 stripped to 3.01", function () {
expect((new bigDecimal("1.505")).multiply(new bigDecimal("2")).stripTrailingZero().getValue()).toBe("3.01");
});
it("should: 4.100 * 2 stripped to 2.05", function () {
expect((new bigDecimal("4.100")).divide(new bigDecimal("2")).stripTrailingZero().getValue()).toBe("2.05");
});
});
28 changes: 28 additions & 0 deletions src/stripTrailingZero.ts
@@ -0,0 +1,28 @@
/*
* Removes zero from front and back*/
export function stripTrailingZero(number) {
const isNegative = number[0] === '-';
if (isNegative) {
number = number.substr(1);
}
while (number[0] == '0') {
number = number.substr(1);
}
if (number.indexOf('.') != -1) {
while (number[number.length - 1] == '0') {
number = number.substr(0, number.length - 1);
}
}
if (number == "" || number == ".") {
number = '0';
} else if (number[number.length - 1] == '.') {
number = number.substr(0, number.length - 1);
}
if (number[0] == '.') {
number = '0' + number;
}
if (isNegative) {
number = '-' + number;
}
return number;
}

0 comments on commit 41a40de

Please sign in to comment.