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

  • Home
  • SEARCH
  • 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 965751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:02:54+00:00 2026-05-16T02:02:54+00:00

It started quite normally. I’m writing complex calculation application for iPhone. I decided to

  • 0

It started quite normally. I’m writing complex calculation application for iPhone. I decided to use float primitive for representing numbers. So, time came to start rounding and formatting output, but first of all rounding. I want to round numbers to different number of decimal places. FOund out there is no useful C function. OK, so I can write my own. But then I have numbers from application settings to persist.
I found out only objects can be persisted.
So I started to dig in NSNumber and NSDecimalNumber. Especially the latter gives me shivers only when look at it. Delegate needed to implement roundings, no smiple arithmetic operators, special methods instead….:(
What is left then of pure C simplicity?

Is there any other way to simplify this?

  • 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-16T02:02:54+00:00Added an answer on May 16, 2026 at 2:02 am

    You could probably use theValue - fmod(theValue, 0.01) to round to, for this example, the nearest 0.01.) But be warned!

    Computers don’t actually work with decimal numbers–i.e. numbers in base ten, the way we use them in normal life. They work with binary numbers, and that includes floating-point numbers. They’re represented in binary as well, and that means that “number of decimal places” is not always a meaningful property of a floating-point number.

    For instance, a floating-point number cannot exactly represent 0.1, and you’ll get something like 0.1000000001 if you try to use it in your code. The exact value you get varies by implementation, and is not correctable by subtracting the difference, as the computer can’t tell that there is a difference–that’s as close as it can get to 0.1.

    This is why classes like NSDecimalNumber exist. They simulate decimal mathematics to give values that seem more sane to us. They’re prone to many of the same problems as floating-point, but with different values (1.0 / 3.0 is not exactly representable in decimal, for instance, but it’s still a discrete value.)

    So yes, if your numbers must be rounded to a particular number of decimal places, you need to use NSDecimalNumber for most decimal operations. If it’s just for display, you can use something like NSNumberFormatter instead to get the display value without modifying the number itself in memory.

    The second part of your question–storing numeric values in user defaults, is easily solved. NSNumber “wraps” a primitive numeric type, but it can be unwrapped:

    NSNumber *n = [NSNumber numberWithDouble: 1.234567890];
    double aDouble = [n doubleValue];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The solution is to use the same code in all… May 16, 2026 at 4:35 am
  • Editorial Team
    Editorial Team added an answer If you're using Sleep in your threads (hence my comment/query… May 16, 2026 at 4:35 am
  • Editorial Team
    Editorial Team added an answer This is impossible using a file upload: You won't have… May 16, 2026 at 4:35 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.