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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:24:44+00:00 2026-05-27T10:24:44+00:00

I have a situation where I have lots of different double values, for example

  • 0

I have a situation where I have lots of different double values, for example 1.00, 0.25 and 2.50. I would like to round these doubles so that they become 1, 0.25 and 2.5; in other words I want to remove any trailing 0’s. Is there a way to do this?

At the moment I have been using %.2f, and I’m wondering if I can make use of this but adapt it in some way. Please can someone help me out?

  • 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-27T10:24:45+00:00Added an answer on May 27, 2026 at 10:24 am

    As long as you’re talking only about display, this is quite easy. The format specifier you want is %g:

    The double argument shall be converted in the style f or e (or in the style F or E in the case of a G conversion specifier), with the precision specifying the number of significant digits […] Trailing zeros shall be removed from the fractional portion of the result[…]

    double twopointfive = 2.500;
    double onepointzero = 1.0;
    double pointtwofive = .25000000000;
    NSLog(@"%g %f", twopointfive, twopointfive);
    NSLog(@"%g %f", onepointzero, onepointzero);
    NSLog(@"%g %f", pointtwofive, pointtwofive);
    

    2011-12-06 21:27:59.180 TrailingZeroes[39506:903] 2.5 2.500000
    2011-12-06 21:27:59.184 TrailingZeroes[39506:903] 1 1.000000
    2011-12-06 21:27:59.185 TrailingZeroes[39506:903] 0.25 0.250000

    The same format specifier can be used with an NSNumberFormatter, which will also give you some control over significant digits.

    The trailing zeroes can’t be removed from the way the number is stored in memory, of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a simple login form (or any form for that
I have a situation where I would like to dynamically create an object through
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation, where running a query that filters by an indexed column in
I have situation like this: user submits form with action='/pay' in '/pay' I have
I have a situation where I have a table that has a soft foreign
I have a situation where I have lists of objects that have to be
I have a Java class that looks like this: public class My_ABC { int
I have a website that is hosted in a different timezone than the users
My situation: I would like to create a hobby project for improving my C++

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.