Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: how to get 123 from 0.123 or generically xyz... from 0.xyz...?

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.

like image 882
hzxu Avatar asked Nov 17 '25 20:11

hzxu


1 Answers

You use the modf function:

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

refer this fractional part of NSDecimalnumber

like image 77
Hector Avatar answered Nov 19 '25 10:11

Hector