Skip to content

Commit

Permalink
add decimal option to percentage function
Browse files Browse the repository at this point in the history
  • Loading branch information
048200987 committed Oct 9, 2024
1 parent a9549f2 commit 8134c5c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-utily",
"version": "1.2.3",
"version": "1.2.4",
"description": "Typescript utils library",
"homepage": "https://github.com/kemotx90/ts-utily#readme",
"main": "dist/ts-utily.js",
Expand Down
16 changes: 10 additions & 6 deletions src/utils/number-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ export const isNumber = <T>(value: T): boolean => {
}
}


/**
* The function calculates the percentage of a value in relation to a total value.
* @param {number} value - The value parameter represents the specific value that you want to calculate the percentage of. It is a number that you want to find the percentage of in relation to the totalValue.
* @param {number} totalValue - The total value represents the maximum value or the total amount that you want to calculate the percentage of.
* @returns a number, which is the calculated percentage value.
* Calculates the percentage of a value relative to a total value, rounded to a specified number of decimal places.
*
* @param {number} value - The part value to calculate the percentage for.
* @param {number} totalValue - The total value that represents 100%.
* @param {number} [decimals=0] - The number of decimal places to round the result to.
* @returns {number} - The calculated percentage.
*/
export const percentage = (value: number, totalValue: number): number => {
return Math.round((value / totalValue) * 100);
export const percentage = (value: number, totalValue: number, decimals: number = 0): number => {
const factor = Math.pow(10, decimals);
return Math.round((value / totalValue) * 100 * factor) / factor;
}
45 changes: 41 additions & 4 deletions test/number-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isNumber, percentage, toNumber} from "../src/index";


// Tests for the toNumber function
test('to number 0, 100, -100, 1.2', () => {
expect(toNumber('0')).toBe(0);
expect(toNumber(100)).toBe(100);
Expand All @@ -15,6 +15,7 @@ test('not number {}, test, u12, 1.2n', () => {
expect(toNumber('1.2n')).toBe(0);
});

// Tests for the isNumber function
test('is number 0, 100, -100, 1.2', () => {
expect(isNumber('0')).toBe(true);
expect(isNumber(100)).toBe(true);
Expand All @@ -31,8 +32,44 @@ test('is not number {}, test, u12, 1.2n', () => {
expect(isNumber(undefined)).toBe(false);
});

test('percentage (10,100), (150.34,300.22), (322,544)', () => {
// Tests for the percentage function
/*
* The `percentage` function calculates the percentage of a value relative to a total value,
* rounded to a specified number of decimal places.
*/

test('percentage of 10 out of 100 is 10%', () => {
expect(percentage(10, 100)).toBe(10);
expect(percentage(150.34, 300.22)).toBe(50);
expect(percentage(322, 544)).toBe(59);
});

test('percentage of 150.34 out of 300.22 is around 50%', () => {
expect(percentage(150.34, 300.22)).toBeCloseTo(50, 0);
});

test('percentage of 322 out of 544 is around 59.19 with 2 decimals', () => {
expect(percentage(322, 544, 2)).toBeCloseTo(59.19, 2);
});

test('percentage of 1 out of 3 is around 33.33 with 2 decimals', () => {
expect(percentage(1, 3, 2)).toBeCloseTo(33.33, 2);
});

test('percentage of 25 out of 25 is 100%', () => {
expect(percentage(25, 25)).toBe(100);
});

test('percentage of 0 out of 25 is 0%', () => {
expect(percentage(0, 25)).toBe(0);
});

test('percentage with 0 total value returns Infinity', () => {
expect(percentage(25, 0)).toBe(Infinity);
});

test('percentage with negative value and total', () => {
expect(percentage(-50, -100)).toBe(50);
});

test('percentage with mixed positive value and negative total', () => {
expect(percentage(50, -200)).toBe(-25);
});

0 comments on commit 8134c5c

Please sign in to comment.