How to get the decimal part of a number in Node.js

1 Answer

0 votes
function getDecimalPart(num) {
    if (Number.isInteger(num)) {
        return 0;
    }
 
    const decimal = num.toString().split('.')[1];
   
    return Number(decimal);
}
 
console.log(getDecimalPart(3.14)); 
console.log(getDecimalPart(126.0)); 
console.log(getDecimalPart(-4.65234)); 
console.log(getDecimalPart(0.1)); 
console.log(getDecimalPart(19));    

   
   
   
/*
run:
   
14
0
65234
1
0
   
*/

 



answered Jun 14, 2022 by avibootz
edited Jun 14, 2022 by avibootz
...