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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:25:59+00:00 2026-06-10T04:25:59+00:00

$ printf %0.2f\n 41.495 41.49 $ printf %0.2f\n 41.485 41.49 $ printf %0.2f\n 41.475

  • 0
$ printf "%0.2f\n" 41.495
41.49
$ printf "%0.2f\n" 41.485
41.49
$ printf "%0.2f\n" 41.475
41.47
$ printf "%0.2f\n" 41.465
41.47
$ printf "%0.2f\n" 41.455
41.46
$ printf "%0.2f\n" 41.445
41.44
$ printf "%0.2f\n" 41.435
41.44
$ printf "%0.2f\n" 41.425
41.42
$ printf "%0.2f\n" 41.415
41.42
$ printf "%0.2f\n" 41.405
41.40

Why are the numbers with an uneven number as the second decimal not correctly rounded and even ones are? Additionally what is wrong with .445 that it never gets rounded?

  • 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-10T04:26:00+00:00Added an answer on June 10, 2026 at 4:26 am

    It has to do with floating-point, but not with double-precision.

    When you write

    printf "%0.2f\n" 41.495
    

    on your system, printf first rounds 41.495 to the closest x87 80-bit floating-point number[1]. How does that work? First write 41.495 in binary:

    b101001.0111 11101011100001010001 11101011100001010001 11101011100001010001 ...
    

    (the separated groups repeat ad infinitum). Now we round this number to have 64 binary digits:

    b101001.0111111010111000010100011110101110000101000111101011100001
    

    This is the number that is actually formatted by printf. Written in decimal, it is exactly:

    41.4949999999999999990285548534529880271293222904205322265625
    

    as you can see, it is just a little bit less than 41.495, so when printf rounds it to two fractional digits, it rounds down, and 41.49 is printed.

    Now look at 41.485; after rounding to 64 binary digits, we get the value:

    41.48500000000000000055511151231257827021181583404541015625
    

    which is just a little bit bigger than 41.485, so printf rounds it up.

    On my system, there is a warning about this in the printf manage:

    Since the floating point numbers are translated from ASCII to floating-point and then back again, floating-point precision may be lost.


    1. bash doesn’t use the x87 format on all operating systems (indeed, it isn’t even available on all architectures); on some other systems these values will be interpreted as doubles (and therefore rounded to 53 bits instead of 64), and the results will differ.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

printf(%d,printf(%*s%*s,6,,6)) Results in the addition of the 2 numbers (6+6), does anyone have on
Take a function like printf that accepts a variable number of arguments what I
The printf() function will return the number of characters printed. But in the code
When we use printf() with varying number of arguments like printf(Hello); printf(%d,a); printf(%d%d,b,c); why
GNU bash, version 4.2.24: $> printf %.0f, %.0f\n 48.5 49.5 48, 50 Ruby 1.8.7
printf returns 1 instead of Hello World! which is the desired result. I googled
Does printf occupy memory in the stack? printf(Hello world); Does Hello world have a
$ printf 'int main(){}' | gcc -static -x c - -o hello $ scp
main() { printf(Hello World.); } Why does no warning is produced in gcc compiler
Why the two printf statements are outputting different values? int main() { int n=10;

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.