Number formatting? Beginner question, decimals.

Discussion in 'Mac Programming' started by Alfons, Jun 1, 2008.

  1. Alfons macrumors newbie

    Joined:
    Mar 15, 2008
    #1
    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.
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    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.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    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.
     
  6. Alfons thread starter macrumors newbie

    Joined:
    Mar 15, 2008
    #6
    Yes, it's objective-C. I think I'll go with the componentsSeparatedByString: method.

    Thanks guys, I got some stuff to try now.:D
     
  7. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #7
    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
     
  8. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    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.

    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...
     
  9. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #9
    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).

    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.
     

Share This Page