Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4080934
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:59:39+00:00 2026-05-20T17:59:39+00:00

I’m using NSDecimalNumber to store a value for currency. I’m trying to write a

  • 0

I’m using NSDecimalNumber to store a value for currency. I’m trying to write a method called “cents” which returns the decimal portion of the number as an NSString with a leading 0 if the number is < 10. So basically

NSDecimalNumber *n = [[NSDecimalNumber alloc] initWithString:@"1234.55"];

NSString *s = [object cents:n];

And I’m trying to craft a method that will return 01, 02, etc…up to 99 as a string. I can’t seem to figure out how to return the mantissa as a string in this format. I feel like I’m missing a convenience method but I looked at the documentation again and don’t see anything jumping out at me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:59:39+00:00Added an answer on May 20, 2026 at 5:59 pm

    Update: This is a relatively old answer, but it looks like people are still finding it. I want to update this for correctness — I originally answered the question as I did simply to demonstrate how one could pull out specific decimal places from a double, but I do not advocate this as a way to represent currency information.

    Never use floating-point numbers to represent currency information. As soon as you start dealing with decimal numbers (as you would with dollars with cents), you introduce possible floating point errors into your code. A computer cannot represent all decimal values accurately (1/100.0, for instance, 1 cent, is represented as 0.01000000000000000020816681711721685132943093776702880859375 on my machine). Depending on which currencies you plan on representing, it is always more correct to store a quantity in terms of its base amount (in this case, cents).

    If you store your dollar values in terms of integral cents, you’ll never run into floating-point errors for most operations, and it’s trivially easy to convert cents into dollars for formatting. If you need to apply tax, for instance, or multiply your cents value by a double, do that to get a double value, apply banker’s rounding to round to the nearest cent, and convert back to an integer.

    It gets more complicated than that if you’re trying to support multiple different currencies, but there are ways of dealing with that.

    tl;dr Don’t use floating-point numbers to represent currency and you’ll be much happier, and more correct. NSDecimalNumber is able to accurately (and precisely) represent decimal values, but as soon as you convert to double/float, you run the risk of introducing floating-point errors.


    This can be done relatively easily:

    1. Get the double value of the decimal number (this will work assuming the number is not too large to store in a double).
    2. In a separate variable, cast the double to an integer.
    3. Multiply both numbers by 100 to account for loss of precision (essentially, convert to cents) and subtract dollars from the original to get the number of cents.
    4. Return in a string.

    (This is a general formula for working with all decimal numbers – working with NSDecimalNumber just requires a little bit of glue code to get it to work).

    In practice, it would look like this (in an NSDecimalNumber category):

    - (NSString *)cents {
        double value = [self doubleValue];
        unsigned dollars = (unsigned)value;
        unsigned cents = (value * 100) - (dollars * 100);
    
        return [NSString stringWithFormat:@"%02u", cents];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.