C++ 设置小数点后的位数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3923202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:04:17  来源:igfitidea点击:

Set the digits after decimal point

c++digits

提问by user474401

I have a float number for example 12.12123 Is there a function which would display only number with 2 digits after decimal point 12.12 ?

我有一个浮点数,例如 12.12123 是否有一个函数可以只显示小数点后 2 位的数字 12.12 ?

Here is the code:

这是代码:

y1 = ( c1 - (a1 * x)) / b1;
 y2 = ( c2 - a2 * x) / b2;

if (y1 == y2)
  cout << "The same";

so if the y1 = 1.001 and the y2 = 1.002 they do not appear as the same.

所以如果 y1 = 1.001 和 y2 = 1.002 它们看起来不一样。

I tried to add. cout.setf(ios::fixed, ios::floatfield); cout.precision(2);

我试着补充。cout.setf(ios::fixed, ios::floatfield); cout.precision(2);

but it does not seem to help.

但它似乎没有帮助。

回答by Matteo Italia

/* The C way */
#include <stdio.h>
...
float f = 12.12123f;
printf("%.2f",f);

// The C++ way
#include <iostream>
...
float f = 12.12123f;
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(2);
std::cout << f;

// The alternative C++ way
#include <iostream>
#include <iomanip>
...
float f = 12.12123f;
std::cout << std::fixed << std::setprecision(2) << f;

In C, the 0 padding is added automatically to the right if there are not enough digits to print. In the C++ examples, instead, this is disabled; to enable this behavior, you should enable the fixed mode on the stream with std::fixed(or enabling the relevant stream flags with std::ios_base::setf()).

在 C 中,如果没有足够的数字可打印,则 0 填充会自动添加到右侧。在 C++ 示例中,这是禁用的;要启用此行为,您应该在流上启用固定模式std::fixed(或启用相关流标志std::ios_base::setf())。

Edit: I remembered wrong; if fixedis not set, the precisionsetting says to the stream the totalnumber of digits to display, including also the ones before the decimal point. So, in this case I think that the only way is to use the fixedmode (examples fixed), which will yield the same behavior of printf.

编辑:我记错了;如果fixed未设置,则precision设置会向流说明要显示的位数,包括小数点之前的位数。因此,在这种情况下,我认为唯一的方法是使用fixed模式(示例已修复),这将产生printf.



Links:

链接:

回答by Frédéric Hamidi

You're looking for printf("%.2f", 12.12123);or:

您正在寻找printf("%.2f", 12.12123);或:

#include <iostream>
#include <iomanip>

using namespace std;
cout << fixed << setprecision(2) << 12.12123;

EDIT:Question changed, so does the answer.

编辑:问题改变了,答案也改变了。

You never want to use direct equality with floating point, you always compare within epsilontolerance. Your epsilonis just quite large.

您永远不想使用浮点直接相等,您总是在epsilon容差内进行比较。你epsilon只是相当大。

Replace if (y1 == y2)with if (abs(y1 - y2) < 0.01).

替换if (y1 == y2)if (abs(y1 - y2) < 0.01)

回答by patentfox

double num = 1.4567;
printf("%.2f",num);

This would print 1.46

这将打印 1.46

回答by dutt

cout.precision(2);
cout <<f;

回答by Armen Tsirunyan

#include <iostream>
#include <iomanip>

int main ()
{
   double d = 1.242354345; 
   using namespace std;
   cout << setiosflags(ios::fixed | ios::showpoint)
        << setprecision(2)
        << d; 
}

回答by patentfox

You are probably asking wrong question. Try following:

你可能问错了问题。尝试以下操作:

double diff = fabs(y1-y2);
if(diff < 0.005)
    cout << "Almost same";

回答by Ruel

cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
float al = 42.645; //sample
cout << al;