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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:25:57+00:00 2026-05-13T09:25:57+00:00

If you put a decimal in a format where has to be rounded to

  • 0

If you put a decimal in a format where has to be rounded to the nearest 10th, and it is: 1.55, it’ll round to 1.5. 1.56 will then round to 1.6. In school I recall learning that you round up when you reach five, and down if it’s 4 or below. Why is it different in Python, et al.

Here’s a code example for Python 2.6x (whatever the latest version is)

'{0:01.2f}'.format(5.555)  # This will return '5.55'

After trying some of the examples provided, I realized something even more confusing:

'{0:01.1f}'.format(5.55)  # This will return '5.5'
# But then
'{0:01.1f}'.format(1.55)  # This will return '1.6'

Why the difference when using 1.55 vs 5.55. Both are typed as literals (so floats)

  • 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-13T09:25:57+00:00Added an answer on May 13, 2026 at 9:25 am

    First off, in most languages an undecorated constant like “1.55” is treated as a double precision value. However, 1.55 is not exactly representable as double precision value, because it doesn’t have a terminating representation in binary. This causes many curious behaviors, but one effect is that when you type 1.55, you don’t actually get the value that’s exactly halfway between 1.5 and 1.6.

    In binary, the decimal number 1.55 is:

    1.10001100110011001100110011001100110011001100110011001100110011001100...
    

    When you type “1.55”, this value actually gets rounded to the nearest representable double-precision value (on many systems… but there are exceptions, which I’ll get to). This value is:

    1.1000110011001100110011001100110011001100110011001101
    

    which is slightly larger than 1.55; in decimal, it’s exactly:

    1.5500000000000000444089209850062616169452667236328125
    

    So, when asked to round this value to a single digit after the decimal place, it will round up to 1.6. This is why most of the commenters have said that they can’t duplicate the behavior that you’re seeing.

    But wait, on your system, “1.55” rounded down, not up. What’s going on?

    It could be a few different things, but the most likely is that you’re on a platform (probably Windows), that defaults to doing floating-point arithmetic using x87 instructions, which use a different (80-bit) internal format. In the 80-bit format, 1.55 has the value:

    1.100011001100110011001100110011001100110011001100110011001100110
    

    which is slightly smaller than 1.55; in decimal, this number is:

    1.54999999999999999995663191310057982263970188796520233154296875
    

    Because it is just smaller than 1.55, it rounds down when it is rounded to one digit after the decimal point, giving the result “1.5” that you’re observing.

    FWIW: in most programming languages, the default rounding mode is actually “round to nearest, ties to even”. It’s just that when you specify fractional values in decimal, you’ll almost never hit an exact halfway case, so it can be hard for a layperson to observe this. You can see it, though, if you look at how “1.5” is rounded to zero digits:

    >>> "%.0f" % 0.5
    '0'
    >>> "%.0f" % 1.5
    '2'
    

    Note that both values round to even numbers; neither rounds to “1”.

    Edit: in your revised question, you seem to have switched to a different python interpreter, on which floating-point is done in the IEEE754 double type, not the x87 80bit type. Thus, “1.55” rounds up, as in my first example, but “5.55” converts to the following binary floating-point value:

    101.10001100110011001100110011001100110011001100110011
    

    which is exactly:

    5.54999999999999982236431605997495353221893310546875
    

    in decimal; since this is smaller than 5.55, it rounds down.

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

Sidebar

Related Questions

The coding like decimal prodprice = Convert.ToDecimal(NavigationContext.QueryString[Price]); then what should i put for Binary
I have a javascript code for textbox that will put commas on in digits
I put together this quick carousel that I'm using to cycle through different divs
I'm looking for a regex or a way to format the NumberValidator so that
Not sure how else to put that, but I'll start off with a code
Hey, I am trying to put a little logic into my C# app that
A program has the output set to 2 decimal place floats, one on each
I have a single that might have a decimal place but might not. I
Im quite new to XSLT and looking at the decimal-format function im still having
I have been using the format: print 'blah, blah %f' %variable to put variables

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.