Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
985 views
in Technique[技术] by (71.8m points)

iphone - Division result is not correct

CGFloat result=100/126;
NSLog(@"%0.5f",result);

the output of above code is 0.00000 but when i check it on calculator then the value is 0.79365. what is the problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is as both operands of division are integers, integer division is performed and its result is 0. To get proper value you need to make sure that at least one of your operands is floating point number:

CGFloat result=100.0f/126;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...