How to get the decimal part of a number in JavaScript

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(48.0)); 
console.log(getDecimalPart(-8.936)); 
console.log(getDecimalPart(0.6)); 
console.log(getDecimalPart(12));    
   
   
   
/*
run:
   
14
0
936
6
0
   
*/

 

 



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

Related questions

1 answer 235 views
4 answers 246 views
2 answers 159 views
2 answers 197 views
...