#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    53
    Rep Power
    11

    Rounding without math.h and functions


    wondering how i can round a double floating point number in C without the math library? Plus i'm having trouble with my functions. I need to call them but i'm just not getting them right.
    They don't display anything after the scanf statment.
    -gman932

    here is my code: w/o rounding..... please tell me where i'm going wrong in my function.

    #include<stdio.h>

    double round (double);

    int main(void) {
    double num, six_num;
    double number; //4th decimal place//

    /*Original floating number*/

    printf("\nEnter a floating point number to be rounded: ");
    scanf("%lf", &num);

    six_num = num * 100000 + 0.5/ 100;

    printf("\n %lf rounded on the fourth decimal place is %lf.", six_num, round (number));

    }

    /*------round func--------*/
    double round (double number) {
    double num;
    number = (num * 1000 + 0.5)/ 100;

    return number;
    }
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    > printf("\n %lf rounded on the fourth decimal place is %lf.", six_num, round (number));
    Here you use number even though you haven't initialised it with a value.

    > number = (num * 1000 + 0.5)/ 100;
    Here you use num, once again you haven't initialised it with a value.

IMN logo majestic logo threadwatch logo seochat tools logo