Results 1 to 4 of 4

Thread: Rounding......

  1. #1

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249

    Rounding......

    What header file and function will let me round in C++, thanks a bunch!

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    None, here is keda's round function:
    inline int round(const double d)
    { return (int)(d + 0.5); }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    jim mcnamara
    Guest
    This rounds as well

    Code:
    sprintf(tmp,"%5.2f",mydouble);
    mydouble=ecvt(tmp);
    the .2 part sets the number of digits after the decimal - and it actually rounds up.

  4. #4
    jim mcnamara
    Guest

    more gernalized rounding function

    a more general rounding function

    Code:
    #include <stdio.h>
    double round(double p, int placesbefore,int placesafter){
    	char tmp[20];
    	sprintf(tmp,"%*.*f",placesbefore,placesafter,p);
    	return ecvt(tmp);
    }
    usage: mydouble = round(mydouble,5,2);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width