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

Rounding float to 2 decimal places 1

Status
Not open for further replies.

Wings

Programmer
Feb 14, 2002
247
0
0
US
Hi,
I need to know how to round a float to 2 decimal places.
lets say this is my code:
Assuming EdtTotalBooks,EdtBookCost, and EdtTotal are edit boxes.

float Temp;
Temp = StrToFloat(EdtTotalBooks->Text);
Temp = Temp * StrToFloat(EdtBookCost->Text);
EdtTotal->Text = FloatToStr(Temp);

For some reason, I get some long float even for simple numbers. For example 6 * 10.1 gives me 60.6000022888184

Any ideas would be helpful,

Thanks
 
1. Multiply by 100 and assign result to an Integer (i1)
2. Assign this Integer(i1) to a float(f1).
3. add 0.5 to f1 to achieve a "real" rounding
4. Divide f1 by 100
hnd
hasso55@yahoo.com

 
try this:

FormatFloat("0.00",Temp);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top