How do I convert a float to an int in Objective C?

111,260

Solution 1

I'm pretty sure C-style casting syntax works in Objective C, so try that, too:

int myInt = (int) myFloat;

It might silence a compiler warning, at least.

Solution 2

what's wrong with:

int myInt = myFloat;

bear in mind this'll use the default rounding rule, which is towards zero (i.e. -3.9f becomes -3)

Solution 3

int myInt = (int) myFloat;

Worked fine for me.

int myInt = [[NSNumber numberWithFloat:myFloat] intValue];

Well, that is one option. If you like the detour, I could think of some using NSString. Why easy, when there is a complicated alternative? :)

Solution 4

You can also use C's lroundf(myFloat).


An incredibly useful tip: In Xcode's editor, type your code as say

myInt = roundf(someFloat);

then control/right-click on roundf and Jump to definition (or simply command-click).

You will then clearly see the very long list of the functions available to you. (It's impossible to remember them all, so just use this trick.)

For example, in the example at hand it's likely that lrintf is what you want.

A further tip: to get documentation on those many functions. In your Terminal.app (or any shell - nothing to do with Xcode, just the normal Terminal.app) simply type man lrintf and it will give you full info. Hope it helps someone.

Solution 5

In support of unwind, remember that Objective-C is a superset of C, rather than a completely new language.

Anything you can do in regular old ANSI C can be done in Objective-C.

Share:
111,260
Nick Locking
Author by

Nick Locking

Updated on July 09, 2022

Comments

  • Nick Locking
    Nick Locking almost 2 years

    Total newbie question but this is driving me mad! I'm trying this:

    myInt = [myFloat integerValue]; 
    

    but I get an error saying essentially integerValue doesn't work on floats.

    How do I do it?

  • Admin
    Admin over 11 years
    but using this I throw away the decimal values, i would like to do like this: myFloat = 0.999 = myInt = 1 but using your code myInt gets 0, how can I do it?
  • Lasse Christiansen
    Lasse Christiansen over 11 years
    @GabrielMolter take a look at roundf - it will round to the nearest integer :)
  • Admin
    Admin over 11 years
    OK! Thank you! Exactly what I need!
  • Eliza Wilson
    Eliza Wilson over 10 years
    ceil (myInt) will also work. Instead of rounding to the nearest, it will just go up. i.e.: ceil (0) is 0, ceil (0.1) is 1;
  • Ky -
    Ky - almost 9 years
    If you're going to turn it into a NSNumber, why not use @(myFloat).intValue?
  • Hermann Klecker
    Hermann Klecker almost 9 years
    @BenC.R.Leggiero, because the answer was given in August 2010 and it is still valid although optimizable. NSNumber literals have benn introduced with LLVM/CLang 3.1 if I am not much mistaken wich was released in 2012. But even then I might have needed some time to learn about it myself. Feel free to add your own answer, dated these days, using literals and you may even earn an upvote from me.