Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I round a number,

Status
Not open for further replies.

Hasch2o

Programmer
Oct 1, 2001
12
0
0
CH
Hello There,

I' ve got a problem with swiss franks :)
i want to round an equal number for ex.
64.363 -> 64.35
64.378 -> 64.40
64.321 -> 64.30
64.999 -> 65.00

I hope you understand my problem,
i tried it with #include <math.h> but
it found no &quot;round&quot;-function

Thx a lot for your help

Hasch2o
 
Hi, I don't know about round method too, so I created one myself. Hope it's all right... s-)

Code:
double round( double dNumber, int nDec, bool bHalfs = false )
{
    const double dPow = pow( 10, nDec );
    if ( bHalfs )
    {		
        const double d0 = floor( dNumber * dPow ) / dPow;
        const double d025 = floor( dNumber * dPow + 0.25 ) / dPow;
        const double d05 = floor( dNumber * dPow + 0.5 ) / dPow;

        if( d025 - d0 > 0.1 )
            return d025;

        else if ( d05 - d0 > 0.1 )
            return d0 + 0.5 / dPow;

        else 
            return d05;			
    }
    else	
        return floor( dNumber * dPow + 0.5 ) / dPow;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top