Answers for "javascript allow number and decimal places"

14

js number 2 decimal places

(3.141596).toFixed(2);	// 3.14
Posted by: Guest on August-13-2020
1

how count decimals javascript

const num1 = 1.123456789;
const num2 = 123456789;
const decimalCount = num => {
   // Convert to String
   const numStr = String(num);
   // String Contains Decimal
   if (numStr.includes('.')) {
      return numStr.split('.')[1].length;
   };
   // String Does Not Contain Decimal
   return 0;
}
console.log(decimalCount(num1)) // 9
console.log(decimalCount(num2)) // 0
Posted by: Guest on August-23-2021

Code answers related to "javascript allow number and decimal places"

Code answers related to "Javascript"

Browse Popular Code Answers by Language