Change a float to int
in Objective-C
The Code
float valueFloat = slider.value;
int valueInt = (int) valueFloat;
Explanation
1-The inital value is received from a slider in the App (slider.value).
- That inital value received is 3.1415 and have 4 decimals (1415).
- That’s what we call in programming a »float ».
2-The slider.value 3.1415 is than transfer to the valueFloat (3.1415).
- Yes they have the same value!
3-The valueFloat 3.1415 is then transfer to (int) valueFloat (3).
- The (int) is saying to the valueFloat to remove the decimals.
- The value is now 3.
4-The valueInt receive the number (3) with no decimal that was cut buy (int) valueFloat.
- Now, you can use a »int » that by his nature does’t support decimals.
Notes:
You can’t transfer a slider value with decimals directly to a »int ».
That’s why we use a »float » as a mediator to access the value.
If you had a value of 3.999 in the world of »int » the result would be 3 (int = 3).