

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Retrieving the decimal part only of a number in JavaScript
Problem
We are required to write a JavaScript function that takes in a number. Our function should pick and return the part of the number after the decimal point(.)
Example
Following is the code −
const num = 435.43436; const retrieveDecimalPart = (num = 1) => { const str = String(num); let [_, decimal] = str.split('.'); const divisor = Math.pow(10, decimal.length) decimal = +decimal; return decimal / divisor; }; console.log(retrieveDecimalPart(num));
Output
Following is the console output −
0.43436
- Related Questions & Answers
- How can I remove the decimal part of JavaScript number?
- Finding length of repeating decimal part in JavaScript
- Sorting only a part of an array JavaScript
- Reversing the bits of a decimal number and returning new decimal number in JavaScript
- Decimal count of a Number in JavaScript
- How to get a decimal portion of a number with JavaScript?
- Finding the only even or the only odd number in a string of space separated numbers in JavaScript
- How to compare only date part without comparing time in JavaScript?
- How to get the port number part of the href attribute of an area in JavaScript?
- Want to fetch only the month part from a date in MySQL
- Check if the given decimal number has 0 and 1 digits only in Python
- What is the octal equivalent of a decimal number in C#?
- MySQL query to change a string by displaying only the part of string after underscore?
- How to round the decimal number to the nearest tenth in JavaScript?
- 10’s Complement of a decimal number?
Advertisements