C++ 用 cout 打印正确的小数点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5907031/
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 19:08:17  来源:igfitidea点击:

Printing the correct number of decimal points with cout

c++

提问by thameera

I have a list of floatvalues and I want to print them with coutwith 2 decimal places.

我有一个float值列表,我想cout用 2 个小数位打印它们。

For example:

例如:

10.900  should be printed as 10.90
1.000 should be printed as 1.00
122.345 should be printed as 122.34

How can I do this?

我怎样才能做到这一点?

( setprecisiondoesn't seem to help in this.)

setprecision这似乎没有帮助。)

回答by beduin

With <iomanip>, you can use std::fixedand std::setprecision

使用<iomanip>,您可以使用std::fixedstd::setprecision

Here is an example

这是一个例子

#include <iostream>
#include <iomanip>

int main()
{
    double d = 122.345;

    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    std::cout << d;
}

And you will get output

你会得到输出

122.34

回答by Vusak

You were nearly there, need to use std::fixed as well, refer http://www.cplusplus.com/reference/iostream/manipulators/fixed/

您快到了,还需要使用 std::fixed,请参阅http://www.cplusplus.com/reference/iostream/manipulators/fixed/

#include <iostream>
#include <iomanip>

int main(int argc, char** argv)
{
    float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 };

    std::cout << std::setprecision(2) << std::fixed;

    for(int i = 0; i < 6; ++i)
    {
        std::cout << testme[i] << std::endl;
    }

    return 0;
}

outputs:

输出:

0.12
1.23
12.35
123.45
1234.50
12345.00

回答by QuantumMechanic

setprecision(n)applies to the entire number, not the fractional part. You need to use the fixed-point format to make it apply to the fractional part: setiosflags(ios::fixed)

setprecision(n)适用于整数,而不是小数部分。您需要使用定点格式使其适用于小数部分:setiosflags(ios::fixed)

回答by einverne

Simplify the accepted answer

简化已接受的答案

Simplified example:

简化示例:

#include <iostream>
#include <iomanip>

int main()
{
    double d = 122.345;
    std::cout << std::fixed << std::setprecision(2) << d;
}

And you will get output

你会得到输出

122.34

Reference:

参考:

回答by Manohar Reddy Poreddy

I had an issue for integers while wanting consistent formatting.

我在想要一致的格式时遇到了整数问题。

A rewrite for completeness:

为完整性重写:

#include <iostream>
#include <iomanip>

int main()
{
    //    floating point formatting example

    double d = 122.345;
    cout << std::fixed << std::setprecision(2) << d << endl;
    //    Output:  122.34


    //    integer formatting example

    int i = 122;
    cout << std::fixed << std::setprecision(2) << double(i) << endl;
    //    Output:  122.00
}

回答by Hargun Singh

I had this similar problem in a coding competition and this is how I handled it. Setting a precision of 2 to all double values

我在编码比赛中遇到了类似的问题,这就是我的处理方式。将所有双精度值设置为 2

First adding the header to use setprecision

首先添加标题以使用 setprecision

#include <iomanip>

#include <iomanip>

Then adding the following code in our main

然后在我们的main中添加以下代码

  double answer=5.9999;
  double answer2=5.0000;
  cout<<setprecision(2)<<fixed;
  cout <<answer << endl;
  cout <<answer2 << endl;

Output:

输出:

5.99
5.00

You need to use fixed for writing 5.00 thats why,your output won't come for 5.00.

您需要使用 fixed 来编写 5.00 这就是为什么,您的输出不会出现 5.00。

A short reference video link I'm adding which is helpful

我添加的一个简短的参考视频链接很有帮助

回答by Eric Z

You have to set the 'float mode' to fixed.

您必须将“浮动模式”设置为固定。

float num = 15.839;

// this will output 15.84
std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;

回答by Hamid Rohani

To set fixed 2 digits after the decimal point use these first:

要在小数点后设置固定的 2 位数字,请先使用这些:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

Then print your double values.

然后打印你的双值。

This is an example:

这是一个例子:

#include <iostream>
using std::cout;
using std::ios;
using std::endl;

int main(int argc, char *argv[]) {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    double d = 10.90;
    cout << d << endl;
    return 0;
}

回答by saurav52

#include<stdio.h>
int main()

{

 double d=15.6464545347;

printf("%0.2lf",d);

}

回答by QuentinUK

with templates

带模板

#include <iostream>

// d = decimal places
template<int d> 
std::ostream& fixed(std::ostream& os){
    os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
    os.precision(d); 
    return os; 
}

int main(){
    double d = 122.345;
    std::cout << fixed<2> << d;
}

similar for scientific as well, with a width option also (useful for columns)

也与科学类似,还有一个宽度选项(对列有用)

// d = decimal places
template<int d> 
std::ostream& f(std::ostream &os){
    os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
    os.precision(d); 
    return os; 
}

// w = width, d = decimal places
template<int w, int d> 
std::ostream& f(std::ostream &os){
    os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
    os.precision(d); 
    os.width(w);
    return os; 
}

// d = decimal places
template<int d> 
std::ostream& e(std::ostream &os){
    os.setf(std::ios_base::scientific, std::ios_base::floatfield); 
    os.precision(d); 
    return os; 
}

// w = width, d = decimal places
template<int w, int d> 
std::ostream& e(std::ostream &os){
    os.setf(std::ios_base::scientific, std::ios_base::floatfield); 
    os.precision(d); 
    os.width(w);
    return os; 
}

int main(){
    double d = 122.345;
    std::cout << f<10,2> << d << '\n'
        << e<10,2> << d << '\n';
}