Skip to main content
added 131 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 40
  • 58

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)

Update: for negative numbers +0.5 should be -0.5

Credits for Edgar Bonet: using round works for both positive and negative numbers:

float f_rounded = round(f * 10) / 10.0;

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)

Update: for negative numbers +0.5 should be -0.5

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)

Update: for negative numbers +0.5 should be -0.5

Credits for Edgar Bonet: using round works for both positive and negative numbers:

float f_rounded = round(f * 10) / 10.0;
added 56 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 40
  • 58

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)

Update: for negative numbers +0.5 should be -0.5

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)

Update: for negative numbers +0.5 should be -0.5

Source Link
Michel Keijzers
  • 13k
  • 7
  • 40
  • 58

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)