Menu

I write coding for rounding, and I confuse something

ian
2013-11-08
2013-11-08
  • ian

    ian - 2013-11-08

    I used Dev c++, and version is 4.9.9.2.

    I write coding for rounding, and I confuse something.

    Please watch my coding in below:

    If I input a=4.15, the output is 4.2

    If I input a=4.25, the output is 4.3

    If I input a=4.35, the output is still 4.3, but it should be 4.4

    Can anyone give me suggestions about this? Thanks.

    include <stdio.h>

    include <stdlib.h>

    int main(){

    float a=0;
    
    scanf("%f",&a);
    
    printf("%f\n\n",(int)(a*10+0.5)/1.0);
    
    system("pause");
    
    return 0;
    

    }

     
  • Merak

    Merak - 2013-11-08

    (int)((a+0.05)*10)/10.0 should work better.. I think

    Although 0.5 is a simple number in decimal, it's not in binary

     

    Last edit: Merak 2013-11-08

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.