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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:40:05+00:00 2026-05-21T10:40:05+00:00

I have a float that I would like to print to exactly N spaces.

  • 0

I have a float that I would like to print to exactly N spaces. For example, with N = 5. I want to get the following numbers printed as so:

0.1        => 0.1
123.456789 => 123.5
-123.45678 => -123 (or -123.)
0.123456   => 0.123
123456.789 => 123456 (obviously, in this case, we cannot print less than 6 digits, if I can detect this, I can also print 12** to indicate the field is too wide.

I have tried many combination including the following:

// Leave 3 extra space: One for negative sign, one for zero, one for decimal
std::cout << std::setiosflags(std::ios::fixed) 
          << std::setprecision(N - 3) 
          << std::setw(N) 
          << input;

But the results are not favorable. Perhaps I am missing something obvious?

The output of the given code for those sample inputs are

 0.10     // not bad 
123.46    // Use 6 instead of 5 spaces
-123.46   // Use 7 instead of 5 spaces
 0.12     // Under-use the spaces
123456.79 // Ok, I guess
  • 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-21T10:40:06+00:00Added an answer on May 21, 2026 at 10:40 am
    int main ()
    {
      const int N = 4;
    
      double f = 123.456789;
    
      // just use default floating-point notation to display N significant digits.
      // no fixed or scientific notation work for your requirement.
      // you can comment setf() line out if there is no one changing ios::floatfield.
      cout.setf(0, ios::floatfield); 
      cout.precision(N);
    
      cout << f << endl;              // will print 123.5
    }
    

    [UPDATE]
    I should have tested for all cases provided. 😀

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    void print_test(double number)
    {
        const int N = 5; // should be greater than 2
    
        double maxIntPart = pow(10.0, number >= 0 ? N - 2 : N - 3);
    
        double fractpart, intpart;
        fractpart = modf(number , &intpart);
    
        ios_base::fmtflags prevNotation = cout.setf(0, ios::floatfield);  // use default floating-point notation
        streamsize prevPrecicion = cout.precision();
    
        if( abs(intpart) > maxIntPart )
        {
            intpart = ceil(number);
            if( abs(intpart) < maxIntPart * 10 )
            {
                cout << intpart << endl;
            }
            else
            {               
                while( abs(intpart) >= maxIntPart )
                {
                    intpart /= 10;
                }
    
                cout << (intpart > 0 ? floor(intpart) : ceil(intpart)) << "**" << endl;
            }
        }
        else
        {
            if ( number < 0 )
            {
                cout.precision(N - 3);
            }
            else if ( intpart == 0)
            {
                cout.precision(N - 2);
            }
            else
            {
                cout.precision(N - 1);
            }
            cout << number << endl;
        }
    
        cout.setf(prevNotation, ios::floatfield); 
        cout.precision(prevPrecicion);
    }
    
    
    int main ()
    {
        print_test(0.1);                // 0.1
        print_test(123.456789);         // 123.5
        print_test(-123.45678);         // -123
        print_test(0.123456);           // 0.123
        print_test(-0.123456);          // -0.12
        print_test(123456.789);         // 123**
        print_test(-123456.789);        // -12**
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have a float value like 1294322.0000000, and I want to print it
I have the following string that I would like to Huffman-encode and store efficiently
I have some C# unit tests that perform some float/double operations and I would
I have a function that returns a float from 0 to 255. I would
So I have a function that looks something like this: float function(){ float x
I have some Objective-C code that looks like this: #define myVar 10 float f
I have double (or float) variables that might be empty, as in holding no
I have a function that receives float** as an argument, and I tried to
Both are mathematical values, however the float does have more precision. Is that the
I have a calculation that generates what appears to be the Float 22.23, and

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.