【问题标题】:Formatting output for floating-point values just when there is fractional values in c++仅当 C++ 中存在小数值时格式化浮点值的输出
【发布时间】:2016-06-10 02:15:45
【问题描述】:

我需要有关如何在 C++ 中格式化输出以显示小数位(如果有)或显示整个整数(如果没有小数点可显示)的帮助。

it should show 95.7 // When there is decimal
it should show 90   // When there is no decimal

我写道:

cout << setprecision(1) << fixed; 

它没有给我预期的输出。它不是打印 90,而是打印 90.0

【问题讨论】:

  • 那么你的问题是什么?你面临什么错误?
  • 简单 std::cout &lt;&lt; 95.7 &lt;&lt; ' ' &lt;&lt; 90.0 &lt;&lt; '\n'; 默认显示您请求的值。你真的连代码都不看吗?如果您的代码不起作用,请将其发布并向我们展示您实际得到的输出。
  • 当我写的时候:cout
  • 当你知道一个数字没有十进制值时,你不应该使用std::setprecision(1),因为它会强制任何数字在小数点右侧有1位数字。您必须首先确定该数字是否具有十进制值。如果有,请使用std::setprecision(1)。否则,不要使用它。
  • @Allen,您确定您使用的是std::cout 而不是printf()

标签: c++ floating-point iomanip


【解决方案1】:

假设你正在执行除法。

  1. 使用cout
    i) 如果小数部分是.000000,那么使用cout 将只打印整数(整数)部分。
    ii) 如果小数部分不是.000000,那么cout 也会打印小数部分。

    因此,在这两种情况下,cout 都会导致您需要的行为。

  2. 使用printf()
    i) 如果小数部分是.000000,则使用printf() 将打印整数,后跟小数部分,例如3.000000。在这种情况下,您将不得不手动处理它,以便您获得的输出只是3。您可以采用不同的方法,例如转换为字符串、使用内置函数等。
    ii) 如果小数部分不是.000000,则printf() 将打印输出,如3.123456

请看下面的代码。我使用的事实是,如果除法的余数为 0,则表示小数部分为 .000000 并在打印之前将数字类型转换为 int。如上所述,您可能必须使用不同的方法。

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    double num1=270.0, num2=90.0;
    cout<<num1/num2<<"\n";                      //prints just 3
    printf("%f\n", num1/num2);                  //prints 3.000000
    if((int)num1%(int)num2==0)                  //means num1 is a multiple of num2.  So decimal is .000000
        printf("%d\n", (int)num1/(int)num2);    

    num2=91.0;
    cout<<num1/num2<<"\n";                      //prints 2.96703
    printf("%f\n", num1/num2);                  //prints 2.967033
    if((int)num1%(int)num2!=0)                  
        printf("%f\n", num1/num2);  
    return 0;
}

现场演示here

【讨论】:

    【解决方案2】:

    您可以创建一个函数,将浮点数转换为小数点后一位。然后测试浮点数是否等于它的整数部分(即如果 90.0 等于 90)然后只打印整数部分,否则打印浮点数。

    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    void printFloat(float num) {
    
        // convert num to properly rounded decimal with 1 decimal place
        num = floor(num*10 + .5)/10; 
    
        // if the float equals the whole number, only show the whole number
        if (num == (int)num) 
            cout << (int)num << endl;
        else
            cout << num << endl;
    }  
    
    int main() {
        float num1 = 90.0;
        float num2 = 90;
        float num3 = 90.00001;
        float num4 = 95.7;
        float num5 = 95.74;
        float num6 = 95.75;
    
        printFloat(num1); // prints 90
        printFloat(num2); // prints 90
        printFloat(num3); // prints 90
        printFloat(num4); // prints 95.7
        printFloat(num5); // prints 95.7
        printFloat(num6); // prints 95.8
    }
    

    【讨论】:

    • if (num == (int)num) cout &lt;&lt; (int)num; else cout &lt;&lt; num; 更短。无需减去即可得到小数部分
    • 感谢@LưuVĩnhPhúc - 我已更新我的答案以反映您更有效的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2011-08-05
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多