Reputation: 79
The solution of the square root of 11 in a normal calculator is roughly
3.31662479036
I am trying round to 3.32
So my problem I am having in iOS is;
In my viewDidLoad
int Mynumber = sqrt(11);
NSLog(@"%d", Mynumber);
I keep getting 3 when I should to get the first three integers. Can anyone help me solve this?
Upvotes: 0
Views: 39
Reputation: 4036
float theFloat = sqrt(11);
NSLog(@"%.2f", theFloat);
// also u can use integer round figur like this...
int rounded = lroundf(theFloat); NSLog(@"%d",rounded);
int roundedUp = ceil(theFloat); NSLog(@"%d",roundedUp);
int roundedDown = floor(theFloat); NSLog(@"%d",roundedDown);
Upvotes: 1
Reputation: 318854
int
is for integers, not decimals. Use float
and %f
.
float myNumber = sqrtf(11);
NSLog(@"%.2f", myNumber);
Upvotes: 3