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!

Rounding a Decimal

Status
Not open for further replies.

ashishsmith

Programmer
Jun 23, 2004
46
US
I want to do this:

convert 3.1425 to 3.14. I know we can output it with the use of set precision but i want to store the value so that it can be used for further calculations.

Say like arr=3.1425
and I want to multiply or plug arr's value in the function with arr=3.14 (rounded off). How can I do this.

Thank you,

Ashish Smith
 
void RoundFloat(float * Data, int Decimals)
{
// Decimals: 2 gives x.yy, 3 gives x.yyy and so forth
// Call like: RoundFloat(&arr,2);
// It works like:
// 1: Make multiply value
// 2: Multiply and convert to long integer
// 3: Divide back and make float
// 4: Write back in original value
float Value = 1.0;
long Working;
for(int Counter = 0;Counter < Decimals;Counter++) Value *= 10.0;
Working = (long)(Data * Value); // Multiply with
Data = (float)Working / Value;
}

Totte
Keep making it perfect and it will end up broken.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top