Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:06:43+00:00 2026-06-18T00:06:43+00:00

I need to calculate the middle point (average) between two real numbers in JavaScript.

  • 0

I need to calculate the middle point (average) between two real numbers in JavaScript. The range of the numbers can vary widely, generally between 10000 and 0.0001.

The naive approach

(parseFloat(first) + parseFloat(second)) / 2

gives me unwanted precission errors, i.e.

(1.1 + 0.1) / 2 = 0.6000000000000001

How can I ensure that the result does not have extra decimal spaces? I guess, since there are two and only two inputs, that the result will need to have maximum one more decimal place than the inputs. So, I need:

 1000 and 3000 to return 2000 (without decimal spaces)
 1234.5678 and 2468.2468 to return 1851.4073
 0.001 and 0.0001 to return 0.00055
 10000 and 0.0001 to return 5000.00005
 0.1 and 1.1 to return 0.6

To clarify: I know all about precision errors and why this happens. What I need is a simple workaround, and I have not been able to find a previous solution on SO.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:06:44+00:00Added an answer on June 18, 2026 at 12:06 am

    You’ll want to use the function toFixed(). Here’s what your code could look like ((1.1 + 0.1) / 2).toFixed(4);. And 4 is the number of decimal spaces.

    Here’s a sample

    var num1 = "123.456";
    var num2 = "456.1235";
    if (num1.split('.')[1].length > num2.split('.')[1].length)
      var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(num1.split('.')[1].length+1);
    else
      var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(num2.split('.')[1].length+1);
    

    And remember inputs are strings, so that’s why num1 and num2 are strings.

    Update: Here’s the correct if statements for your desired results.

    var num1 = "123.456";
    var num2 = "456.1235";
    if (num1.split('.').length == 1) {
      if (num2.split('.').length == 1)
        var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(0);
      else
        var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(num2.split('.')[1].length);
    } else if (num2.split('.').length == 1)
      var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(num2.split('.')[1].length);
    else if (num1.split('.')[1].length == num2.split('.')[1].length)
      var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(num2.split('.')[1].length);
    else if (num1.split('.')[1].length > num2.split('.')[1].length)
      var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(num1.split('.')[1].length+1);
    else if (num1.split('.')[1].length < num2.split('.')[1].length)
      var x = ((parseFloat(num1) + parseFloat(num2)) / 2).toFixed(num2.split('.')[1].length+1);
    

    EDIT I had to change the order it checked for numbers with no decimal space.
    Here’s a jsFiddle I created to help with the testing: link

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate the distance (in meters and miles) between two coordinates given
I need to calculate the distance between two places the mouse was clicked on
I need to calculate the difference between two timestamps in milliseconds. Unfortunately, the DateDiff-function
I need to calculate the within and between run variances from some data as
I have a table and I need calculate two aggregate functions with different conditions
I need to calculate changes off of time series of nullable numbers. The following
I need to calculate the number of days passed between 2 dates using java.util.Date,
I need to calculate the are of overlap between polygons in the same table.
I need to calculate the factorial of numbers up to around 100! in order
I need to calculate the overlap (amount or yes/no) that two rectangles make on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.