Menu

Need help- small error, rounding function

2002-11-07
2012-09-26
  • Nobody/Anonymous

    hey everybody,
    curt again.
    making a function that rounds real numbers to the tens and hundredths place, but i keep getting a small error.  whenever i enter a real number with decimal places only out to the hundredths place, it subtracts 1/100 from the final answer for seemingly no reason.

    Here's the source of my little program: // The user input "real" is the real number that is rounded.
    //
    // The function hundFunction(double real) rounds the user's input to
    // the hundredths place.
    //
    // The function tenFunction(double real) round the user's input to
    // the tens place.

    #include <iostream.h>
    #include <iomanip.h>
    #include <string>

    double real;
    double hundFunction(double real), tenFunction(double real);

    main()
    {
        cout << "Enter a real number: ";
        cin >> real;
        cout << endl << hundFunction(real);
        cout << endl << tenFunction(real);
        system("pause");
        return 0;
    }

    double hundFunction(double real)
    {
       
        double realHund, x;     
        int hund, hunddec;
        hund=real;                      // hund drops the decimal from real
                                        //
        realHund=(real-hund)*100;       // realHund finds the decimal places
                                        // to the hundredths off of 'real'
                                        //
        hunddec=realHund;               // hunddec ensures that any additional
                                        // decimal places are truncated
                                        //
        x = hund + (hunddec * 0.01);    // x is a double that moves hunddec
                                        // to the hundredths place by multiplying
                                        // it by 0.01 and adding it to the original
                                        // truncated integer hund
        return x;
    }

    double tenFunction(double real)
    {
        double realTen;
        int ten, tendec;
        ten=real;
        realTen=(real-ten)*10;
        tendec=realTen;
        return ten+(tendec*0.1);
    }

    *********************************************************

    keep in mind that i'm not worrying about even rounding up or down on decimal places beyond the hundredths place, i'm only worrying about why it subtracts a single one-hundredth from the final answer when a user only inputs a real number to the hundredths place.

    thanks again.

    --curt

     
    • Nobody/Anonymous

      UPDATE: i realized that the error only occurs when the final decimal place that i enter in the hundredths position is less than 5

       
    • Nobody/Anonymous

      another update- the error only happens when a number that only extends to the hundredths place and the final digit is between 2 and 4

      help!

       

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.