设置小数点后的位数

发布于 2024-09-26 22:42:28 字数 350 浏览 1 评论 0原文

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

代码如下:

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

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

因此,如果 y1 = 1.001 和 y2 = 1.002,它们看起来并不相同。

我尝试添加。 cout.setf(ios::fixed, ios::floatfield); cout.精度(2);

但这似乎没有帮助。

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 ?

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.

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

but it does not seem to help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一百个冬季 2024-10-03 22:42:28
/* 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;

在 C 中,如果没有足够的数字可供打印,则会自动在右侧添加 0 填充。 在 C++ 示例中,此功能被禁用;要启用此行为,您应该使用 std::fixed (或使用 std::ios_base::setf())。

编辑: I记错了;如果未设置fixed,则precision设置向流表示要显示的位数,还包括小数点之前的位数。因此,在这种情况下,我认为唯一的方法是使用固定模式(固定示例),这将产生与 printf 相同的行为。


链接:

/* 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()).

Edit: I remembered wrong; if fixed is not set, the precision setting says to the stream the total number 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 fixed mode (examples fixed), which will yield the same behavior of printf.


Links:

梦中楼上月下 2024-10-03 22:42:28

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

#include <iostream>
#include <iomanip>

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

编辑: 问题已更改,答案也已更改。

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

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

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

#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 epsilon tolerance. Your epsilon is just quite large.

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

煮酒 2024-10-03 22:42:28
double num = 1.4567;
printf("%.2f",num);

这将打印 1.46

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

This would print 1.46

南渊 2024-10-03 22:42:28
cout.precision(2);
cout <<f;
cout.precision(2);
cout <<f;
澉约 2024-10-03 22:42:28

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

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

You are probably asking wrong question. Try following:

double diff = fabs(y1-y2);
if(diff < 0.005)
    cout << "Almost same";
平定天下 2024-10-03 22:42:28
#include <iostream>
#include <iomanip>

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

int main ()
{
   double d = 1.242354345; 
   using namespace std;
   cout << setiosflags(ios::fixed | ios::showpoint)
        << setprecision(2)
        << d; 
}
自此以后,行同陌路 2024-10-03 22:42:28
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
float al = 42.645; //sample
cout << al;
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
float al = 42.645; //sample
cout << al;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文