round() function

This is a discussion on round() function within the C++ Programming forums, part of the General Programming Boards category; if this can round to an int.... int x = floor( your_float_or_double + 0.5 ); then it can be made ...

  1. #16
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if this can round to an int....

    int x = floor( your_float_or_double + 0.5 );

    then it can be made to work for a float something like....

    float x = floor( 100*(your_float_or_double + 0.5) ) / 100.0;

    notice how we move the decimal point two places by multiplying with 100 so that floor can find an int then dividing result by 100 gives decimal to 2 decimal places. Thats what u want isnt it.See it wasnt hard. You were almost given the answer by your teacher and i tried not to give it to u but instead make you think for yourself but as you couldn't work it out I have shown you the guts.
    Firstly wrap in a function.
    Secondly a bit harder, can you make a function that will round to a passed number of decimal points.Its not too hard.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  2. #17
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    yeah i kinda realized it just now :/

    this is what i did

    Code:
    f=floor(d*100)/100.0;
    soz, but thx
    the early bird gets the worm but the second mouse gets the cheese

  3. #18
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    that wont round it will floor.
    To round either floor(d+0.5) or ceil(d-0.5).

    That extra 0.5 is important. This is because flooring 4.6 will give 4 but flooring 4.6+0.5 will give 5 a correct rounding off.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21