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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:55:15+00:00 2026-05-31T15:55:15+00:00

I have a float value, that I would like to show it in format

  • 0

I have a float value, that I would like to show it in format string and if it corresponds to an int, showing the integer, if not, showing it with one decimal.

Like this :

3.1
3
2.9
2.8

For now I’m stuck, in the concept, I’d do something like that :

float myFloat = 3.1
float mySecondFloat = 3
[NSString stringWithFormat:@"%g %g", myFloat, mySecondFloat];

My question is:

1/ the type format “%g” works in most cases but sometimes i have result shown like “0.600001” while in reality there should be 0.6 because all I do is 0.7 – 0.1.

Is there a kind of type cast for float at 1 decimal or maybe a bitwise operation to get rid of the final imprecision, or other way to make it works in 100% of cases ?

Thanks for your answers.

  • 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-31T15:55:16+00:00Added an answer on May 31, 2026 at 3:55 pm

    If your need absolute precision when working with decimal numbers, you may consider using the NSDecimalNumber class.

    Number and Value Programming Topics: Using Decimal Numbers

    Otherwise, the %.1g format specifier will be OK.

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

Sidebar

Related Questions

I would like to format a float number as a percent-value with JFormattedTextField that
I have a string value that could contain a double, integer, ascii or byte
I have float field that contains (for example): 1234.5678. I would like to display
I have a class (from NSObject ) that contains: NSString name int position float
I would like to store a mapping of an integer key to a float
I would like to calculate the color depending on a percentage value: float percentage
I would like to have a slot that calls the function of the appropriate
Both are mathematical values, however the float does have more precision. Is that the
I have a (float) rating value as a percentage from 0..100 (where 50 =
I have an iPhone app. I am storing a float value, (distance), in my

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.