2

As title states, if I have a float, I need to get the fraction part as an integer, how do I do it?

I was thinking:

  1. get index(position) of decimal point
  2. then I can know how many digits after decimal point
  3. get those digits as substring
  4. convert it to an integer

is there any better/smarter way?

update:

I forgot to mention, the float has format like: X.YZ so there are at most two digits after decimal point.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
hzxu
  • 5,753
  • 11
  • 60
  • 95
  • 1
    What is your exact requirement? If you're using the float type, even a number like 12 might have an exact value of 12.000001. If the number is captured from the user, you should probably be using a char* or NSString instead. – Rajesh J Advani Jul 31 '12 at 06:37

8 Answers8

9

You use the modf function:

double integral;
double fractional = modf(some_double, &integral);

refer this fractional part of NSDecimalnumber

Community
  • 1
  • 1
Hector
  • 3,909
  • 2
  • 30
  • 46
5
 float temp=12.123;
NSString *str=[NSString stringWithFormat:@"%f",temp];
NSArray *arr=[str componentsSeparatedByString:@"."];
int tempInt=[[arr lastObject] intValue];
Nikunj
  • 987
  • 11
  • 25
  • I picked your answer but I need to make some change in my case, as the 'temp' is a returned value from some method, it may be 12.12300001, but I only need 123 or even 12, so I changed %f to %.2f and removed the last 0 if there is any. – hzxu Jul 31 '12 at 07:17
  • hmm need change as per your above requirement – Nikunj Jul 31 '12 at 07:40
4
NSString *str = [NSString stringWithFormat:@"%f", value];
NSArray *arr = [str componentsSeparatedByString:@"."];
int num = [[arr objectAtIndex:1] intValue];
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
2

Let take a example: x = 129.567;

convert x into integer and put in y. y = int(x); so y = 129;

now subtract y from x. so z = x-y; z = .567

z = z*1000; so z = 567

I think thats what you are looking for.

Rahul Bhansali
  • 135
  • 1
  • 1
  • 8
  • @Hector for 129.5678, after getting .5678 you can find the length of your fraction and multiply 10 that many times. What say??? – Rahul Bhansali Jul 31 '12 at 06:55
  • But I don't know how many digits after ., so if it is 1.1, then I will get 1000 instead of 1 – hzxu Jul 31 '12 at 07:07
  • @HZXU you have to find the length of fraction and multiply 10 that no. of times. – Rahul Bhansali Jul 31 '12 at 07:10
  • @HZXU As you are saying there at most 2 digit after decimal point. So you can check X.YZ whether z is 0 or non zero. if z is 0 multiply by 10 else multiply by 100. – Rahul Bhansali Jul 31 '12 at 07:18
1

How about taking the float and multiply by 1000 and convert the result to an integer?

Tommy Hui
  • 1,306
  • 6
  • 9
0

Take a look at NSDecimalNumber and NSDecimal.

Julien
  • 3,427
  • 20
  • 22
0

Float never represents a number with accuracy. The best way is to create a string from "double" number and then do the string manipulation to get the digits after decimal point.

Apurv
  • 17,116
  • 8
  • 51
  • 67
0

Here's my solution:

float value = 12.345f;
float integral;
float divisor = 1000.0f;
float fractional = modff(value, &integral);  // breaks a float into fractional and integral parts

int xyz = (int)fmodf(fractional*divisor, divisor);  // modulo (cannot just use %)

NSLog(@"XYZ: %d", xyz); // logs "XYZ: 345"

By changing the value of divisor you can get the precision you want, e.g. only XY (100.0f) or X (10.0f).

The solution is not the most performant, but useful. I think it could be optimized using bitwise operations.

Notes on using "f":

Community
  • 1
  • 1
matm
  • 7,059
  • 5
  • 36
  • 50