Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
How to get a decimal portion of a number with JavaScript?
With JavaScript, use the % operator to get the decimal portion of a number.
Example
You can try to run the following to get decimal portion −
<!DOCTYPE html>
<html>
<body>
<script>
var num1 = 5.3;
var num2 = 4.2;
var num3 = 8.6;
document.write(num1 % 1);
document.write("<br>"+num2 % 1);
document.write("<br>"+num3 % 1);
</script>
</body>
</html>
Output
0.2999999999999998 0.20000000000000018 0.5999999999999996
Advertisements