
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Counting numbers after decimal point in JavaScript
We are required to write a JavaScript function that takes in a number which may be an integer or a floating-point number. If it's a floating-point number, we have to return the count of numbers after the decimal point. Otherwise we should return 0.
For our example, we are considering two numbers −
const num1 = 1.123456789; const num2 = 123456789;
Example
Following is the code −
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
Output
This will produce the following output in console −
9 0
- Related Articles
- Counting smaller numbers after corresponding numbers in JavaScript
- Counting the clusters of positive numbers - JavaScript Arrays
- How to validate decimal numbers in JavaScript?
- Counting n digit Numbers with all unique digits in JavaScript
- Counting specific digits used in squares of numbers using JavaScript
- How to remove the digits after the decimal point in axis ticks in Matplotlib?
- C# program to remove all the numbers after decimal places
- How can we validate decimal numbers in JavaScript?
- JavaScript algorithm for converting Roman numbers to decimal numbers
- How to Align a Column of Numbers by Decimal Point in Excel?
- Counting prime numbers from 2 upto the number n JavaScript
- What is Counting numbers ?
- Counting How Many Numbers Are Smaller Than the Current Number in JavaScript
- Counting largest numbers in row and column in 2-D array in JavaScript
- Why are natural numbers called counting numbers?

Advertisements