[英]How many ways to show decimals are there?
So i was making a program to solve a math exercise (quadratic equation in english i think).所以我正在制作一个程序来解决数学练习(我认为是英文的二次方程)。 The site's examples gave me 1.25 0 -1.25 and the output 1.00 and -1.00.该站点的示例给了我 1.25 0 -1.25 和输出 1.00 和 -1.00。
Made the program, but only got 5/100 (because only the "no root for the equation" worked).制作了程序,但只有 5/100(因为只有“方程无根”有效)。 The problem was that the output didn't display .00 so i searched for a way to show decimals, when i stumbled across问题是输出没有显示 .00,所以我偶然发现了一种显示小数的方法
std::cout.precision(2);
std::cout.setf(std::ios::fixed);
which worked and got 100/100.它有效并获得了 100/100。
So are there easier ways to do this?那么有没有更简单的方法来做到这一点? I'm a beginner at c++.我是 C++ 的初学者。 If not, at least can somebody explain those 2 lines to me?如果没有,至少有人可以向我解释这两行吗?
If you如果你
#include <iomanip>
you can use:您可以使用:
std::cout << std::setprecision(2) << std::fixed << n;
I would go the long way and convert the floating point number to a string and make my own precision routines up.我会走很长的路,将浮点数转换为字符串,并制作我自己的精度例程。 This is the problem in college and I would be making my own routines up.这是大学里的问题,我会制定自己的惯例。 Using precision floating point was not in my beginning c++ class so you have to point to your reference of study.使用精度浮点数不在我开始的 C++ 类中,所以你必须指出你的学习参考。 I got docked down because I knew too much for the simple class and did not reference my sources.我被停职是因为我对简单的课程了解太多,并且没有参考我的资料来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.