### Implementing a rounding function

Hello, I have created a rounding function, and i need help implementing it on a row of data that I have.

 ``1234567891011`` `````` float round_value(float value, int decimal_places) { int magnitude = 1; for (int i = 0; i = decimal_places; i++) { magnitude = magnitude * 10; } value = value * magnitude; value = int (value + .5); value = value / magnitude; return value; }``````

I want to implement it into this code:
 ``123456789`` ``````for(row = 0; row < MAX_ROWS; row++) { rowAverage[row]=0; for(col = 0; col < MAX_COLS; col++) { rowAverage[row]=rowSum[row]/MAX_ROWS; } }``````

I tried this just after it, but it doesn't work, I thought it would take the value in the row, apply the rounding function then put it back into there.

 ``1234`` ``````for(row=0; row < MAX_ROWS;row++) { rowAverage[row]=round_value(rowAverage[row], DECIMAL_PLACES); }``````

DECIMAL_PLACES = 5
Bump
So what it doing or not doing?

What you've shown in the third snippet looks like it should work assuming your round_value function is correct and that rowAverage is declared as:
` float rowAverage[MAX_ROWS];`

Have you displayed the before and after values to see what you're getting?
Last edited on
Is this a typo? `for (int i = 0; i = decimal_places; i++) {`
You are trying to do the work of the hardware by using the hardware. Forget the loop.

Also, an ancient article on simple rounding algorithms that I hope to update someday:
http://www.cplusplus.com/forum/articles/3638/

Hope this helps.
Stop spamming.

http://www.cplusplus.com/forum/beginner/110686/
http://www.cplusplus.com/forum/beginner/110693/
and this.

Also learn to edit your post and don't bump every 40 minutes.
Topic archived. No new replies allowed.