3

Possible Duplicate:
Objective-c: How to round off Float values?

I know its with the NSNumberformatter class, any sample code please?

Community
  • 1
  • 1
user988618
  • 49
  • 1
  • 1
  • 2
  • From what I can tell using the NSNumberformatter is the way to go, but I can't seem to find any good examples. – user988618 Oct 11 '11 at 07:22

2 Answers2

20

Since ints can't store floating-point numbers, the decimal portion of a float will be truncated and you will be left with the whole number:

float x = 3.14159;
int y = (int)x;

This works because the int is only capable of storing integers, so it simply stores the integer portion of x


See also gnu C Library Rounding Functions:

 — Function: double ceil (double x)
 — Function: float ceilf (float x)
 — Function: long double ceill (long double x)
 — Function: double floor (double x)
 — Function: float floorf (float x)
 — Function: long double floorl (long double x)
 — Function: double trunc (double x)
 — Function: float truncf (float x)
 — Function: long double truncl (long double x)
 — Function: double rint (double x)
 — Function: float rintf (float x)
 — Function: long double rintl (long double x)
 — Function: double nearbyint (double x)
 — Function: float nearbyintf (float x)
 — Function: long double nearbyintl (long double x)
 — Function: double round (double x)
 — Function: float roundf (float x)
 — Function: long double roundl (long double x)
 — Function: long int lrint (double x)
 — Function: long int lrintf (float x)
 — Function: long int lrintl (long double x)
 — Function: long long int llrint (double x)
 — Function: long long int llrintf (float x)
 — Function: long long int llrintl (long double x)
 — Function: long int lround (double x)
 — Function: long int lroundf (float x)
 — Function: long int lroundl (long double x)
 — Function: long long int llround (double x)
 — Function: long long int llroundf (float x)
 — Function: long long int llroundl (long double x)
 — Function: double modf (double value, double *integer-part)
 — Function: float modff (float value, float *integer-part)
 — Function: long double modfl (long double value, long double *integer-part)
chown
  • 51,908
  • 16
  • 134
  • 170
  • 2
    If the OP really did want rounding (and not truncation as you pointed out), perhaps using `ceil()` would be helpful here. – Aidan Steele Oct 11 '11 at 00:50
  • @SedateAlien Your right, it just took me a few minutes to paste in those functions and the link. SO on an iPhone is not the quickest :). – chown Oct 11 '11 at 00:57
  • ... and your pile of functions are from the _GNU C Library_ not the _Apple Objective C Runtime_. Maybe some are available, but I certainly wouldn't be shocked if many aren't available. – sarnold Oct 16 '11 at 07:30
  • @sarnold Obj-C is a [superset](http://en.m.wikipedia.org/wiki/Subset#_) of C. – chown May 07 '12 at 22:13
  • @chown: OS X is derived from **BSD** sources; not all functions from the **GNU C Library** are going to be available on OS X. If that list of functions were from a POSIX standard or ISO C standard, I'd have more faith that they would also be available on OS X. (Though [not all POSIX-specified functions work](http://stackoverflow.com/questions/1413785/sem-init-on-os-x#comment7265381_1452182)...) – sarnold May 07 '12 at 23:17
11

But 3.999 is also equal to 3 if you just cast it to int.

float value = 3.14159f;
int intValue = (int)value;
float fractional = fmodf(value, (float)intValue);
if(fractional > .5f)
    intValue++;

intValue is your rounded int.

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114