14

It might be a simple solution but I can not fix it.

I am dividing 2 integers :

finishedGameFinalScore = [score integerValue];
CGFloat interval = 2/finishedGameFinalScore;
NSLog(@"interval = %f",interval);

The log returns 0.000000

Is there a limit for decimal places? I need to preserve the decimal result.

Thanks Shani

Dustin
  • 6,783
  • 4
  • 36
  • 53
shannoga
  • 19,649
  • 20
  • 104
  • 169
  • 1
    `CGFloat finishedGameFinalScore = [score floatValue];` `CGFloat interval = 2.0f/finishedGameFinalScore;` – janusfidel Aug 15 '12 at 12:30
  • 1
    You have to divide by a floating pointer number to get a floating point number. – Joe Aug 15 '12 at 12:31
  • 1
    This is true for every C-based language. Dividing two ints results in an int. – borrrden Aug 15 '12 at 12:32
  • Possible duplicate of [iOS int always returning 0](https://stackoverflow.com/questions/21102750/ios-int-always-returning-0) – phuclv Oct 31 '17 at 05:10

4 Answers4

74

The reason your code doesn't work is that you're dividing an integer by another integer and then casting the result to a float.

So you have 2 (an integer) and some other number (also an integer). Then you divide 2 by this number - which is probably greater than 2. Let's say it's 3.

Integer sees 2/3 and he's like "0.66666667? Pshh, no one ever needs anything after the decimal point anyway". So he truncates it. You just have 0.

Then Integer gives the number to Mr. float and Mr float is super happy to get a number! He's all like "yay, a 0! I'm going to add ALL OF THE SIGNIFICANT DIGITS". And that's how you end up with 0.0000000.

So yeah, just cast to a float first. Or even a double!

enter image description here

Dustin
  • 6,783
  • 4
  • 36
  • 53
  • Of even just do `CGFloat interval = 2.0/finishedGameFinalScore;` (add the `.0`). As `2.0` is a `CGFloat`, the result will be a `CGFloat` keeping all the decimals. – Alejandro Iván Feb 16 '16 at 15:07
13

@Dustin said u will need to typecast your divider value to float as it goes in float it shows integer value

CASE 1: Typecast

NSString *score = @"3";
 int interval = [str intValue];
 CGFloat interval = (2/(float)interval);
 NSLog(@"interval = %.2f",interval);

CASE 2: No need for typecast

NSString *score = @"3";
 float interval = [str floatValue];
 CGFloat interval = (2/interval);
 NSLog(@"interval = %.2f",interval);
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
8

Just add the f-hint to the number 2. in this case that will do the trick.

CGFloat interval = 2.0f/finishedGameFinalScore;

all the above/below answers are correct and fully explain why this work.

Dan1one
  • 1,288
  • 8
  • 13
  • Normally, you wouldn't add this as a new answer if there are already two answers unless it adds significant important data. You could comment it on the question or another answer if you think it really adds something. But I'll +1 it because you're new. – Dustin Aug 15 '12 at 16:40
1

just devide long value from 1.0 and assigned to float variable.

unsigned long l1 = 65536;
unsigned long l2 = 256;
float f = (l1/1.0)/(l2/1.0);

NSLog(@"%f",f);
Subhash
  • 545
  • 7
  • 11