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 1066683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:03:13+00:00 2026-05-16T20:03:13+00:00

If I have this little function: <script type=text/javascript> function printFloat(){ var myFloatNumber1 = document.getElementById(‘floatNumber1’);

  • 0

If I have this little function:

<script type="text/javascript">

 function printFloat(){
      var myFloatNumber1 = document.getElementById('floatNumber1');
      var myFloatNumber2 = document.getElementById('floatNumber2');
      alert(parseFloat(myFloatNumber1.value) + parseFloat(myFloatNumber2.value))
 }

</script>

<input type="text" id="floatNumber1"></input>
<input type="text" id="floatNumber2"></input>

<input type="button" onclick="printFloat()"/>

in field 1 I enter: 221.58
in field 2 I enter: 2497.74

I expect the sum of 2 numbers in the input fields to be a 2 number digit: 2719.32
But the result is a incorrect number… : 2719.3199999999997

a round would do the job, but I just don’t get why the code does that on this number…
On other number combinations, the sum is correct…

  • 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-05-16T20:03:13+00:00Added an answer on May 16, 2026 at 8:03 pm

    From The Floating-Point-Guide:

    Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and
    instead I get a weird result like
    0.30000000000000004?

    Because internally, computers use a
    format (binary floating-point) that
    cannot accurately represent a number
    like 0.1, 0.2 or 0.3 at all.

    When the code is compiled or
    interpreted, your “0.1” is already
    rounded to the nearest number in that
    format, which results in a small
    rounding error even before the
    calculation happens.

    In your case, the rounding errors happen when the values you entered are converted by parseFloat().

    Why do other calculations like 0.1 + 0.4 work correctly?

    In that case, the result (0.5) can be
    represented exactly as a
    floating-point number, and it’s
    possible for rounding errors in the
    input numbers to cancel each other out
    – But that can’t necessarily be relied upon (e.g. when those two numbers were
    stored in differently sized floating
    point representations first, the
    rounding errors might not offset each
    other).

    In other cases like 0.1 + 0.3, the
    result actually isn’t really 0.4, but
    close enough that 0.4 is the shortest
    number that is closer to the result
    than to any other floating-point
    number. Many languages then display
    that number instead of converting the
    actual result back to the closest
    decimal fraction.

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

Sidebar

Related Questions

I have an external JS file that I load into my HTML <script type=text/javascript
I have this function in C# to convert a little endian byte array to
I have this little text <div> Hello world<br> this is a good<br> text! <br><br>
I have this little script that shows one wisdom each day. so I have
I have this little script to toggle a contact form when a button is
I am playing a little with some non-blocking JavaScript loading. This means I have
I have this little rake task: namespace :db do namespace :test do task :reset
I have this little problem, that I cannot figure out which arguments to pass
I have this little question that's been on my mind for a while now.
I have this little problem. My client wanted two distinct swf on a web

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.