-
-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathdecimal_isolation.ts
More file actions
23 lines (19 loc) · 740 Bytes
/
decimal_isolation.ts
File metadata and controls
23 lines (19 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @function isolateDecimal
* @description Isolate the decimal part of a number.
* @param {number} num - The number to isolate the decimal part from.
* @returns {Object} - Object containing the integral part and the decimal part.
* @throws {TypeError} - when the input is not a number.
* @example isolateDecimal(3.14) => { integralPart: 3, decimalPart: 0.14 }
*/
export const isolateDecimal = (
num: number
): { integralPart: number; decimalPart: number } => {
if (typeof num !== 'number') {
throw new TypeError('Input must be a number')
}
num = Math.abs(num)
const integralPart = Math.trunc(num)
const decimalPart = num - integralPart
return { integralPart, decimalPart: Number(decimalPart.toFixed(10)) }
}