Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Alfons

macrumors newbie
Original poster
Mar 15, 2008
10
0
Hi all!

I looked all over the Xcode documentation and the Internet, but I can't find what I am looking for, anywhere. Surely, there is a simple solution to this problem I have. I wan't to use only the decimal part of a float, is there a simple way to do this?

Say I got a float 3,14. I want to seperate 14 from the 3 and use both in different functions. Can someone point me in the direction of a primer about juggling with numbers in Xcode?

Any help is welcome!
Alfons the noob.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I assume we are using C (or Objective C) here.

This should work (I've not tested it though):
Code:
float originalNumber = 3.14;
int wholePart;
float fractionalPart;
wholePart = (int) originalNumber;
fractionalPart = originalNumber-wholePart;

At the end wholePart should be 3, fractionalPart should be 0.14.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
if it is C-like, try fmod w/ 1.0 as the 2nd operator for the decimal part, then floor for the whole number.

-Lee
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
If you want to get 3 and 14 returned and the number is a string you can use a string splitter such as NSString's componentsSeparatedByString: with the separator as a dot.
 

Alfons

macrumors newbie
Original poster
Mar 15, 2008
10
0
Yes, it's objective-C. I think I'll go with the componentsSeparatedByString: method.

Thanks guys, I got some stuff to try now.:D
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
I would strongly suggest going with robbie duncan's way. Using a formatter to convert to a string then separating the string and converting back to a number is much more work (for you and your system) than just using math functions and scalar values. It could also be a problem if the program is used with different international settings (that use something other than . for the symbol) unless you spend the time configuring the formatter first.

Once you start forming habits, they can be hard to get away from when you get into larger apps. You are probably going to need the math.h functions sometime, so you might as well learn them soon.

Code:
float value = 3.14;
float fractionOnly = value - floorf(value);

The functions can be helpful when you need to round values to specific increments. To round for time increments for example:

Code:
float value = 48;
float precision = 15;
float roundedValue = roundf(value / precision) * precision;

All I usually use are floor(), ceil(), and round(). For the float version, just add "f" to the end. ie. floorf(), ceilf(), roundf().

Best of luck
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
I would strongly suggest going with robbie duncan's way. Using a formatter to convert to a string then separating the string and converting back to a number is much more work (for you and your system) than just using math functions and scalar values.

He wants the three and 14 not 3 and 0.14 I think, and that is much harder if you don't do it the string way. Anyway you shouldn't ever performance tweak until you know if it matters, but you should just do it in the easiest way.

It could also be a problem if the program is used with different international settings (that use something other than . for the symbol) unless you spend the time configuring the formatter first.

Good point, Europeans use , for example. You should get the number separator rather than assume its a . I believe you can do this with
Code:
NSNumberFormatter *nf=[[NSNumberFormatter alloc] init];
[nf decimalSeparator];

This will need testing however...
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
He wants the three and 14 not 3 and 0.14 I think, and that is much harder if you don't do it the string way.

True enough. It does depend on whether Alfons is always working with the same precision. I can't think of many instances where .14 and .000014 should be considered the same number, but I am sure cases exist.

If the precision is known, you can always get the number with roundf(value * 10 * precision).

Anyway you shouldn't ever performance tweak until you know if it matters, but you should just do it in the easiest way.

Agreed about performance tweaking, but we are discussing unwritten code in this case. Performance implications should be considered in planning, though they don't need to be the primary objective. Besides, "the easiest way" could mean quite a few things. If an application is any more than an exercise, the developer has a responsibility to his/her users to understand the tools available for the job.

My apologies for the attitude in my last post, the best way does depend on the situation. That said, new Cocoa developers (including myself years ago) often think that everything needs to go through the NS, CG, CA, etc classes in order to be appropriate for the application.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.