• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Convert Double to Integer

Using JavaScript to Convert Double to Integer

September 29, 2022 Leave a Comment

We can use JavaScript to convert a double to an integer by using the JavaScript parseInt() method.

var num = parseInt(20.12345);

The above code would return the number 20.


The parseInt() JavaScript method will parse the value given to it and then return the Integer value. If no integer is found at the start, NaN will be returned.

Some other examples of the parseInt() method are below:

var num1 = parseInt(40.7245);
var num2 = parseInt(.273983678);
var num3 = parseInt(-1.28980);
var num4 = parseInt(-.93898);
var num5 = parseInt("$11.345");

console.log(num1);
console.log(num2);
console.log(num3);
console.log(num4);
console.log(num5);

#Output
40
0
-1
0
NaN

Note the parseInt() JavaScript method is similar to the parseFloat() method, the main difference being that parseInt() will only return the Integer, while parseFloat() will return decimal values as well.

Convert Double to Integer using JavaScript

Below we will provide code to let the user input a double, and then use the parseInt() method to return the Integer. Here is our simple HTML setup:

<p>Type a number you want to use the parseInt() method on below:</p>
<input id="userVal" type="number" value="">
<div id="click-me"  onclick="convertDoubleToInt()">Convert to Integer</div>
<div id="results"></div>

Below is the JavaScript code which takes the user input using an onclick event, and uses the parseInt() method on that user input. We update the results below using the textContent property.

function convertDoubleToInt(){
  document.getElementById("results").textContent = parseInt(Number(document.getElementById("userVal").value));
};

The final code and output for this example is below:

Code Output:

Type a number you want to use the parseInt() method on below:

Convert to Integer

Full Code:

<p>Type a number you want to use the parseInt() method on below:</p>
<input id="userVal" type="number" value="">
<div id="click-me"  onclick="convertDoubleToInt()">Convert to Integer</div>
<div id="results"></div>

<script>

function convertDoubleToInt(){
  document.getElementById("results").textContent = parseInt(Number(document.getElementById("userVal").value));
};

</script>

Hopefully this article has been useful in helping you understand how to use JavaScript to convert a double to int.

Other Articles You'll Also Like:

  • 1.  JavaScript Square Root with Math.sqrt() Method
  • 2.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 3.  Using JavaScript to Remove Substring From String
  • 4.  How to Exit for Loop in JavaScript
  • 5.  JavaScript if else Shorthand Statement
  • 6.  JavaScript slice – How to Get a Substring From a String
  • 7.  Sum the Digits of a Number in JavaScript
  • 8.  Uncheck a Radio Button Using JavaScript
  • 9.  JavaScript Round to Nearest 10
  • 10.  Using JavaScript to get Textarea Value

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x