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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:02:59+00:00 2026-06-02T21:02:59+00:00

I need to print a float value in area of limited width most efficiently.

  • 0

I need to print a float value in area of limited width most efficiently. I’m using an NSNumberFormatter, and I set two numbers after the decimal point as the default, so that when I have a number like 234.25 it is printed as is: 234.25. But when I have 1234.25 I want it to be printed as: 1234.3, and 11234.25 should be printed 11234.

I need a maximum of two digits after the point, and a maximum of five digits overall if I have digits after the point, but it also should print more than five digits if the integer part has more.

I don’t see ability to limit the total number of digits in NSNumberFormatter. Does this mean that I should write my own function to format numbers in this way? If so, then what is the correct way of getting the count of digits in the integer and fractional parts of a number? I would also prefer working with CGFLoat, rather than NSNumber to avoid extra type conversions.

  • 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-02T21:03:00+00:00Added an answer on June 2, 2026 at 9:03 pm

    Here is how I implemented this in my code. I don’t know how efficient it is, I hope not bad.

    So I create a global NSNumberFormatter

    NSNumberFormatter* numFormatter; 
    

    and initialize it somewhere:

    numFormatter=[[NSNumberFormatter alloc]init];
    

    Then I format number with the following function:

    - (NSString*)formatFloat:(Float32)number withOptimalDigits:(UInt8)optimalDigits maxDecimals:(UInt8)maxDecimals
    {
        NSString* result;
        UInt8 intDigits=(int)log10f(number)+1;
        NSLog(@"Formatting %.5f with maxDig: %d maxDec: %d intLength: %d",number,optimalDigits,maxDecimals,intDigits);  
    
        numFormatter.maximumFractionDigits=maxDecimals;
        if(intDigits>=optimalDigitis-maxDecimals) {
            numFormatter.usesSignificantDigits=YES;
            numFormatter.maximumSignificantDigits=(intDigits>optimalDigits)?intDigits:optimalDigits;
        } else {
            numFormatter.usesSignificantDigits=NO;
        }
        result = [numFormatter stringFromNumber:[NSNumber numberWithFloat:number]];
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to format float value and I only need 2 numbers after point
i need to print out numbers 1-100 in a random order. the print statement
I need to print a receipt from my web based apps using dot matrix
I need to print the ASCII value of the given character in awk only.
I need to print XML that I manipulated using groovy and maintain the attribute
On Python 2.5 I need to use float numbers with a modified __str__() method.
We need to print thousands of invoices that are in this format - http://example.com/orders/n
I need to print the below string which is in Arabic on TextView in
I need to print out data into a pre-printed A6 form (1/4 the size
I need to print the date in the format of mm/dd/yyyy. if the date

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.