1

How does one convert an unsigned long number to a float in Objective-C?

Eric Christensen
  • 819
  • 3
  • 9
  • 21

2 Answers2

5

Typecast it the same as in C. Typecasting is explicitly converting one data type to another.

unsigned long myLong = 5;
float myFloat = (float)myLong;

(float)myFloat is the key. The datatype (i.e. float, int) goes in the parentheses and directly after it comes the variable you're typecasting.

http://docs.hp.com/en/B3901-90007/ch05s05.html has further information.

Walker
  • 1,215
  • 2
  • 13
  • 26
3

Objective-C is remarkably similar to C. In fact, it can be implemented as a layer on top of standard C with runtime support for the creation and destruction of objects.

What that generally means is that most things you can do in C, you can also do in Objective-C. Casting an unsigned long to a float is done (in both languages) as follows.

unsigned long ul = 7;
float f = (float)ul;
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953