Rounding of floats in c

Discussion in 'QnA (read only)' started by ravix, Jun 30, 2006.

Thread Status:
Not open for further replies.
  1. ravix

    ravix New Member

    Joined:
    Apr 28, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    we all know that c rounds off floats while using them. can anybody tell me about the rule it follows.:confused:
     
  2. JGuru

    JGuru Well-Known Member

    Joined:
    Dec 25, 2005
    Messages:
    1,726
    Likes Received:
    32
    Trophy Points:
    48
    Location:
    Space-time continuum
    Oops , I moved to Java long time back. I forgot C Language. I need to refer a good
    C book!!
     
  3. deathvirus_me

    deathvirus_me New Member

    Joined:
    May 15, 2005
    Messages:
    1,838
    Likes Received:
    4
    Trophy Points:
    0
    Location:
    Kolkata, India
    Hmmm ... wasn't there a function like Abs or something ???
     
  4. casanova

    casanova The Frozen Nova

    Joined:
    Sep 25, 2004
    Messages:
    1,641
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Trespasser in Virtual Land
    [quote="deathvirus_me]Hmmm ... wasn't there a function like Abs or something ???[/quote]
    Abs gives us the absolute value
    eg
    abs (-12)=12
    abs(12.24)=12.00

    The rounding of depends on the precision. Have never peeped into all this stuff. But floats have precision upto 6 decimals. And if you need to print say floats in 2 decimals, u can use %.2f as format specifier
     
  5. Ishan

    Ishan New Member

    Joined:
    Jun 12, 2005
    Messages:
    366
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Bharuch,Gujarat,India
    I know this in C++..inclue the math.h
    cout.precision(2);
    cout<<1.2345;
    OUTPUT
    1.23

    Clear?
     
  6. bukaida

    bukaida New Member

    Joined:
    Jul 21, 2005
    Messages:
    326
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    KOLKATA
    You can use the floor and ceil function also to control the round off to next higher or lower digit.
     
Thread Status:
Not open for further replies.

Share This Page