techman9
techman9

Reputation: 79

Getting all decimals from interger being Squarooted

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

Answers (2)

Ilesh P
Ilesh P

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

rmaddy
rmaddy

Reputation: 318854

int is for integers, not decimals. Use float and %f.

float myNumber = sqrtf(11);
NSLog(@"%.2f", myNumber);

Upvotes: 3

Related Questions