加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

仅使用标准C将任何浮点数打印到N个空格

发布时间:2020-12-16 09:34:07 所属栏目:百科 来源:网络整理
导读:我有一个浮动,我想打印到N个空格.例如,N = 5.我想得到以下数字: 0.1 = 0.1123.456789 = 123.5-123.45678 = -123 (or -123.)0.123456 = 0.123123456.789 = 123456 (obviously,in this case,we cannot print less than 6 digits,if I can detect this,I can a
我有一个浮动,我想打印到N个空格.例如,N = 5.我想得到以下数字:

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.

我尝试了很多组合,包括以下内容:

// 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;

但结果不利.也许我错过了一些明显的东西?

这些样本输入的给定代码的输出是

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

解决方法

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]
?我应该测试所提供的所有案例. :d

#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**
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读