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 7832257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:07:15+00:00 2026-06-02T12:07:15+00:00

Possible Duplicate: Rounding numbers in Objective-C objective -C : how to truncate extra zero

  • 0

Possible Duplicate:
Rounding numbers in Objective-C
objective -C : how to truncate extra zero in float?
Correcting floating point numbers

In Objective C, I have a double which receives the answer to a calculation as shown:

double answer = a / b;

The double variable can sometimes be a whole integer but can also be a fraction e.g.

NSLog(@"%f", answer)

This can return 2.000000 or 5.142394 or 2.142000.

I was wondering if there was a way to edit the decimal places so that the trailing 0’s are not visible for example:

2.000000 would be 2
5.142394 would be 5.142394
2.142000 would be 2.142

How can this be done?

  • 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-06-02T12:07:16+00:00Added an answer on June 2, 2026 at 12:07 pm

    If you’d like to hard-code the number of decimal places you’re after, this would give you two decimal places:

    NSLog(@"%.2f", answer);
    

    Read more about format specifiers here:

    http://www.cplusplus.com/reference/clibrary/cstdio/printf/

    Alternatively, if you’d like to change the number of decimal places on-the-fly, consider using the NSNumberFormatter class:

    int maxDigitsAfterDecimal = 3; // here's where you set the dp
    
    NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
    [nf setMaximumFractionDigits:maxDigitsAfterDecimal];
    
    NSString * trimmed = [nf stringFromNumber:[NSNumber numberWithDouble:3.14159]];
    NSLog(@"%@", trimmed); // in this case, 3.142
    
    [nf release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Rounding numbers in Objective-C In Objective-c How to round off Float values?
Possible Duplicate: Python rounding error with float numbers I have a rounding Problem in
Possible Duplicate: Floating point inaccuracy examples PHP rounding issue - Is this a bug?
Possible Duplicate: What is a simple example of floating point/rounding error? When I execute
Possible Duplicate: Why are these numbers not equal? The below expression, which evaluates to
Possible Duplicate: C# Double - ToString() formatting with two decimal places but no rounding
Possible Duplicate: how to make “pretty rounding” in R? I have a number, say
Possible Duplicates: Is JavaScript's Math broken? Java floating point arithmetic I have the current
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Is JavaScript's Math broken? Strange result with floating point addition Some simple

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.