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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:34:39+00:00 2026-06-10T19:34:39+00:00

I am looking into rounding floating point numbers in Python and the following behavior

  • 0

I am looking into rounding floating point numbers in Python and the following behavior seems quite strange:

Code:

a = 203.25
print '%.2f'%(a/10.)
print '%.2f'%(round(a/10., 2))
print '%.2f'%(0.1*a)

Output:

20.32
20.32
20.33

Why does the first and especially the second case fail?

  • 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-10T19:34:40+00:00Added an answer on June 10, 2026 at 7:34 pm

    http://en.wikipedia.org/wiki/Rounding#Round_half_to_even

    Round half to even
    A tie-breaking rule that is less biased is round half to even, namely:

    If the fraction of y is 0.5, then q is the even integer nearest to y.
    Thus, for example, +23.5 becomes +24, as does +24.5; while −23.5
    becomes −24, as does −24.5.

    This method treats positive and negative values symmetrically, and is
    therefore free of sign bias. More importantly, for reasonable
    distributions of y values, the expected (average) value of the rounded
    numbers is the same as that of the original numbers. However, this
    rule will introduce a towards-zero bias for even numbers, and a
    towards-infinity bias for odd ones.

    This variant of the round-to-nearest method is also called unbiased
    rounding, convergent rounding, statistician’s rounding, Dutch
    rounding, Gaussian rounding, odd-even rounding
    or bankers’
    rounding
    , and is widely used in bookkeeping.

    This is the default rounding mode used in IEEE 754 computing functions
    and operators.

    >>> "%.2f"%20.325
    '20.32'
    >>> "%.2f"%20.335
    '20.34'
    >>> "%.2f"%20.345
    '20.34'
    >>> "%.2f"%20.355
    '20.36'
    

    So the real question should be why does the third case fail?

    203.25 can be expressed exactly in the floating point representation, however 0.1 cannot, it turns out to be a tiny bit more than 0.1

    >>> 0.1*203.25
    20.325000000000003
    

    So it gets rounded up

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

Sidebar

Related Questions

I have been looking into the source code of python-mpdor and it mentions that
Im looking into documenting the javascript code we develop and are looking for some
I am looking into the pointers and I came up with code like that
I'm looking for an algorithm to segment a sequence of positive numbers into n
Client looking into using QR codes in print advertising that will reward the visitor
Looking into the .NET code, Rectangle.Size returns new Size(Width, Height) . Why did Microsoft
I have been looking into reducing the memory footprint of an application. Following on
I'm currently looking into options for process monitoring of Rails/Ruby Projects and quite like
Looking into Eclipse 4 RCP. Did the following: Created an Eclipse Plugin Project (no
I am basically looking into a buggy makefile, which confused me with the following

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.