简体   繁体   English

有多少种显示小数的方法?

[英]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.

相关问题 如何移动小数并显示特定数字? C++ - How to move decimals and show specific number? C++ sprintf太多/很少的小数 - sprintf too many/few decimals C ++:编译器以多种方式优化我们的代码? - C++ : In how many ways compiler optimizes away our code? 我可以用多少种不同的方式调用函数 - In how many different ways can I call a function 在C ++中调用(调用)函数的方式有多少? - In how many ways can a function be invoked(called) in C++? 有多少种方法可以将两个因素组成一组? (C / C ++) - How many ways to make groups of 2 of factors of a number? (C/C++) 有多少种生成临时对象/不必要调用构造函数的方法? - How many ways of generating temporary objects/needlessly invoking constructor are there? 如何删除/不显示第8个小数点后的浮点小数 - How to erase/don't show the float decimals after the 8.th one 给定一个数字列表,有多少种方法来获得21? 如何获得所有可能的数字组合? - How many ways to get 21 given a list of numbers? How to get all posible combinations of numbers? 给定 N 个范围。 有多少种方法可以从这些总和为某个值的范围中取出 N 个数字? - Given N ranges. How many ways are there to take N numbers from these ranges that sums up to a certain value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM