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:
Round off a number to the next multiple of 5 using JavaScript
Next article icon

How to add float numbers using JavaScript ?

Last Updated : 31 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two or more numbers the task is to get the float addition in the desired format with the help of JavaScript.

There are two methods to solve this problem which are discussed below:

Table of Content

  •  Using parseFloat() and toFixed() method
  • Using parseFloat() and Math.round() method
  • Using Number() and Intl.NumberFormat method

 Using parseFloat() and toFixed() method

  • Given two or more numbers, to sum up the float numbers.
  • Use parseFloat() and toFixed() method to format the output accordingly.

Example: This example implements the above approach. 

JavaScript
let val = parseFloat('2.3') + parseFloat('2.4'); console.log("2.3 + 2.4 = " + val);  function gfg_Run() {     console.log("2.3 + 2.4 = "         + (parseFloat('2.3') +             parseFloat('2.4')).toFixed(2)); } gfg_Run() 

Output
2.3 + 2.4 = 4.699999999999999 2.3 + 2.4 = 4.70 

Using parseFloat() and Math.round() method

  • Given two or more numbers, to sum up the float numbers.
  • Use parseFloat() and Math.round() method to get the desired output.

Example: This example implements the above approach. 

JavaScript
let val = parseFloat('2.3') + parseFloat('2.4'); console.log("2.3 + 2.4 = " + val);  function gfg_Run() {     console.log("2.3 + 2.4 = " +         Math.round((parseFloat('2.3')             + parseFloat('2.4')) * 100) / 100); } gfg_Run() 

Output
2.3 + 2.4 = 4.699999999999999 2.3 + 2.4 = 4.7 

Using Number() and Intl.NumberFormat method

Given two or more numbers, to sum up the float numbers:

  • Use Number() to convert strings to floating-point numbers.
  • Use Intl.NumberFormat to format the output accordingly.

Example:

JavaScript
let val = Number('2.3') + Number('2.4'); console.log("2.3 + 2.4 = " + val);  function gfg_Run() {     let sum = Number('2.3') + Number('2.4');     let formattedSum = new Intl.NumberFormat('en-US',      { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(sum);     console.log("2.3 + 2.4 = " + formattedSum); } gfg_Run(); 

Output
2.3 + 2.4 = 4.699999999999999 2.3 + 2.4 = 4.70 



Next Article
Round off a number to the next multiple of 5 using JavaScript

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-math
  • JavaScript-Numbers
  • 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