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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Round

Status
Not open for further replies.

mik92

Programmer
Jul 4, 2001
3
IT
How are you?
i have a little problem, like all?
I don't know how i can set the decimals. for example if i have a edit->text, and i write 1.12x i want to print 1.13 if x>5 or 1.12 if x<5.
Please help me?
Bye,
Mik
 
wait, do you write '1.12x', or '1.12(and the number)' Cyprus
 
float s;
int intvalue;
s=1.12x;
intvalue=(int)(s*100+0.5); Consider Type Conversion
s= (float)intvalue/100.0;

hnd
hasso55@yahoo.com

 
I write '1.12(and the number)'.
my problem is if I must execute more calculuses with numbers with 3 decimal and round them to the second decimal
Thanks,
Miky
 
This should do it for you. It is not fully tested but I did try it on my compiler and it seemed to work fine.

Just verify it :)

Matt


double round(double value, int decimalPlace)
{
int multiplier = 10;

for(int i = 0;i<decimalPlace;i++)
{
multiplier*=10;
}

long temp = (long)(value*multiplier);
double retVal;

if(temp%10 >= 5)
{
temp+=10;
}

temp/=10;
multiplier/=10;
retVal = temp;
retVal /= multiplier;
return retVal;

}




 
in the case of 4.9999 rounded you will see 5 printed out. Look into &quot;set precision&quot;

Matt
 
I haven't any words as..... Thank you,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top