Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Responsive time of an event
Next article icon

How to Convert a Float Number to the Whole Number in JavaScript?

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a float number and the task is to convert a float number to a whole number using JavaScript.

Below are various methods to convert float numbers to whole numbers in JavaScript:

Table of Content

  • Math.floor (floating argument)
  • Math.ceil (floating argument)
  • Math.round (floating argument)
  • Math.trunc (floating argument)
  • parseInt (floating argument)
  • double bitwise not (~~) operator
  • JavaScript bitwise OR (|) Operator
  • Using shift (>>) operator
  • Using unsigned shift (>>>) operator
  • By subtracting the fractional part
  • Using XOR (^) operator

Math.floor (floating argument)

Round off the number passed as a parameter to its nearest integer in the Downward direction. 

Syntax:

Math.floor(value);

Example: In this example, we are using the above-explained method.

javascript
// float value is 4.59; let x = 4.59; let z = Math.floor(x); console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 4.59 is 4

Math.ceil (floating argument)

Return the smallest integer greater than or equal to a given number. 

Syntax:

Math.ceil(value);

Example: Here is the basic use of the Math.ceil() method.

javascript
// float value is 4.59; let x = 4.59; let z = Math.ceil(x); console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 4.59 is 5

Math.round (floating argument)

Round a number to its nearest integer. 

Syntax:

Math.round(var);

Example: Here we are using the above-explained method to convert numbers.

javascript
// float value is 4.59; let x = 4.59; let z = Math.round(x); console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 4.59 is 5

Math.trunc (floating argument)

Return the integer part of a floating-point number by removing the fractional digits. 

Syntax:

Math.trunc(value);

Example: Here we are using Math.trunc() method to remove the fractional digits.

javascript
// float value is 4.59; let x = 4.59; let z = Math.trunc(x); console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 4.59 is 4

parseInt (floating argument)

Accept the string and convert it into an integer. 

Syntax:

parseInt(Value, radix);

Example: Here is basic example of above method.

javascript
// float value is 3.54; let x = 3.54; let z = parseInt(x); console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 3.54 is 3

double bitwise not (~~) operator

Round a number to zero. If an operand is a number and it’s not NaN or Infinity. 

Syntax:

~~value

Example: Here is the example of a not(~~) operator.

javascript
// float value is 4.59; let x = 4.59; let z = ~~x; console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 4.59 is 4

JavaScript bitwise OR (|) Operator

Round a number towards zero. 

Syntax:

let = value | 0;

Example: Here is an example of the above-explained method.

javascript
// float value is 5.67; let x = 5.67; let z = x | 0; console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 5.67 is 5

Using shift (>>) operator

Round a number to towards zero. 

Syntax:

let = value >> 0;

Example: Basic example of shift(>>) operator.

javascript
// float value is 5.63; let x = 5.63; let z = x >> 0; // It is same as we are dividing the value by 1. console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 5.63 is 5

Using unsigned shift (>>>) operator

Round a number to towards zero. 

Syntax:

let = value >>> 0;

Example: Here is an example of the above-explained method.

javascript
// float value is 5.68; let x = 5.68; // It is same as we are dividing the value by 1. let z = x >>> 0; console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 5.68 is 5

By subtracting the fractional part

In this we substract the fractional part

Syntax:

let = val - val%1;

Example: In this example, we are using the above-explained method.

javascript
// float value is 5.48; let x = 5.48; let z = x - x % 1; console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 5.48 is 5

Using XOR (^) operator

Syntax:

let = value ^ 0;

Example: Here is the basic use of the XOR(^) operator.

javascript
// float value is 5.49; let x = 5.49; let z = x ^ 0; console.log("Converted value of " + x + " is " + z); 

Output:

Converted value of 5.49 is 5


Next Article
JavaScript Responsive time of an event

A

AmanAgarwal6
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions
  • javascript-math
  • JavaScript-Questions

Similar Reads

  • JavaScript Program to print multiplication table of a number
    In this article, we are given a number n as input, we need to print its table. Examples: Input : 5 Output : 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 Input : 8 Output : 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 4
    4 min read
  • What is the use of Math object in JavaScript ?
    The Math object in JavaScript provides a set of methods and properties for mathematical constants and functions. It is a built-in object that allows for performing mathematical operations and accessing mathematical constants without creating an instance. What is the Math Object?The Math object is an
    4 min read
  • How to get median of an array of numbers in JavaScript ?
    In this article, we will see how to find the median of an array using JavaScript. A median is a middle value in a given set of numbers or data. Examples: Input arr1 : [1 4 7 9]Output : 5.5Explanation : The median of the two sorted array is the middle elements which is 5 if the arrayLength =arr1.leng
    3 min read
  • How to calculate greatest common divisor of two or more numbers/arrays in JavaScript ?
    In this article, we are given two or more numbers/array of numbers and the task is to find the GCD of the given numbers/array elements in JavaScript. Examples: Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {2, 4, 6, 8} Output : 2 The GCD of three or more numbers equals the product of the prim
    2 min read
  • How to test a value x against predicate function and returns fn(x) or x in JavaScript ?
    In this article, we are given a value x, and the task is to check the value against the predicate function. If the value satisfies the condition or predicate return fn(x) else return x. Predicate functions are the function that takes one item as input and returns true or false based on the whether i
    3 min read
  • How to Flatten a Given Array up to Specified Depth in JavaScript?
    Flatten an array in JavaScript is a common task when dealing with nested arrays. The concept of "flatten" means to convert a multi-dimensional array into a single-dimensional array. When you need to control the depth of flattening, JavaScript provides methods to handle this efficiently. This process
    2 min read
  • How to get the standard deviation of an array of numbers using JavaScript ?
    Given an array and the task is to calculate the standard deviation using JavaScript. Example: Input: [1, 2, 3, 4, 5]Output: 1.4142135623730951Input: [23, 4, 6, 457, 65, 7, 45, 8]Output: 145.13565852332775Please refer to Mean, Variance, and Standard Deviation for details. Mean is average of element.
    3 min read
  • How to evaluate binomial coefficient of two integers n and k in JavaScript ?
    The following are the common definitions of Binomial Coefficients. A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n. Binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects
    2 min read
  • How to check two numbers are approximately equal in JavaScript ?
    In this article, we are given two numbers and the task is to check whether the given numbers are approximately equal to each other or not. If both numbers are approximately the same then print true otherwise print false. Example: Input: num1 = 10.3 num2 = 10 Output: true Approach: To check whether t
    2 min read
  • How to create slider to map a range of values in JavaScript ?
    The task is to map a range of values to another range of values like map (0-100) to (100-10000000) in JavaScript. There are two approaches that are discussed below. Approach 1: Use the logarithmic scale to map the range. In this example, first, the log value of the range is calculated and then the s
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences