Skip to content

Commit a2a46ab

Browse files
committed
Update version to 4.7.0, add countDecimalPlaces function for counting decimal places in numbers, and document changes in CHANGELOG.md and README.md. Include tests for countDecimalPlaces functionality.
1 parent b9026c6 commit a2a46ab

6 files changed

Lines changed: 73 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# deverything
22

3+
## 4.7.0
4+
5+
### Minor Changes
6+
7+
- countDecimalPlaces
8+
39
## 4.6.0
410

511
### Minor Changes

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Contributions always welcome!
7777
### Math
7878

7979
- `average()`
80+
- `countDecimalPlaces()` count the number of decimal places in a number
8081
- `isBetween()`
8182
- `isOutside()`
8283
- `isStrictlyBetween()`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deverything",
3-
"version": "4.6.0",
3+
"version": "4.7.0",
44
"description": "Everything you need for Dev",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, test } from "vitest";
2+
import { countDecimalPlaces } from "./countDecimalPlaces";
3+
4+
describe("countDecimalPlaces", () => {
5+
test("integers", () => {
6+
expect(countDecimalPlaces(0)).toBe(0);
7+
expect(countDecimalPlaces(1)).toBe(0);
8+
expect(countDecimalPlaces(100)).toBe(0);
9+
expect(countDecimalPlaces(-5)).toBe(0);
10+
});
11+
12+
test("decimals", () => {
13+
expect(countDecimalPlaces(1.5)).toBe(1);
14+
expect(countDecimalPlaces(1.23)).toBe(2);
15+
expect(countDecimalPlaces(1.234567)).toBe(6);
16+
expect(countDecimalPlaces(0.1)).toBe(1);
17+
expect(countDecimalPlaces(0.123456789)).toBe(9);
18+
});
19+
20+
test("negative decimals", () => {
21+
expect(countDecimalPlaces(-1.5)).toBe(1);
22+
expect(countDecimalPlaces(-1.23)).toBe(2);
23+
expect(countDecimalPlaces(-0.123)).toBe(3);
24+
});
25+
26+
test("scientific notation", () => {
27+
expect(countDecimalPlaces(1e-5)).toBe(5);
28+
expect(countDecimalPlaces(1.23e-2)).toBe(4);
29+
expect(countDecimalPlaces(1.5e2)).toBe(0);
30+
});
31+
32+
test("edge cases", () => {
33+
expect(countDecimalPlaces(Infinity)).toBe(0);
34+
expect(countDecimalPlaces(-Infinity)).toBe(0);
35+
expect(countDecimalPlaces(NaN)).toBe(0);
36+
});
37+
});

src/math/countDecimalPlaces.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Counts the number of decimal places in a number.
3+
* @example
4+
* countDecimalPlaces(1.23); // 2
5+
* countDecimalPlaces(1.234567); // 6
6+
* countDecimalPlaces(5); // 0
7+
* countDecimalPlaces(0.1); // 1
8+
*/
9+
export const countDecimalPlaces = (num: number): number => {
10+
if (!Number.isFinite(num)) return 0;
11+
12+
const numStr = num.toString();
13+
14+
// Handle scientific notation
15+
if (numStr.includes("e")) {
16+
const [, exponent] = numStr.split("e");
17+
const exp = parseInt(exponent, 10);
18+
const [base] = numStr.split("e");
19+
const basePlaces = base.includes(".") ? base.split(".")[1].length : 0;
20+
return Math.max(0, basePlaces - exp);
21+
}
22+
23+
// Handle regular decimal notation
24+
if (!numStr.includes(".")) return 0;
25+
26+
return numStr.split(".")[1].length;
27+
};

src/math/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./average";
2+
export * from "./countDecimalPlaces";
23
export * from "./isBetween";
34
export * from "./isOutside";
45
export * from "./isStrictlyBetween";

0 commit comments

Comments
 (0)