簡體   English   中英

使用 cout 打印正確的小數位數

[英]Printing the correct number of decimal points with cout

我有一個float值列表,我想用帶 2 個小數位的cout打印它們。

例如:

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

我怎樣才能做到這一點?

setprecision似乎對此沒有幫助。)

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

這是一個例子

#include <iostream>
#include <iomanip>

int main()
{
    double d = 122.345;

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

你會得到輸出

122.34

您快到了,還需要使用 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;
}

輸出:

0.12
1.23
12.35
123.45
1234.50
12345.00

setprecision(n)適用於整數,而不是小數部分。 您需要使用定點格式使其適用於小數部分: setiosflags(ios::fixed)

簡化接受的答案

簡化示例:

#include <iostream>
#include <iomanip>

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

你會得到輸出

122.34

參考:

我在想要一致的格式時遇到了整數問題。

為完整性重寫:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    //    floating point formatting example
    cout << fixed << setprecision(2) << 122.345 << endl;
    //    Output:  122.34

    //    integer formatting example
    cout << fixed << setprecision(2) << double(122) << endl;
    //    Output:  122.00
}

我在編碼比賽中遇到了類似的問題,這就是我處理它的方式。 將所有雙精度值設置為 2

首先添加標題以使用 setprecision

#include <iomanip>

然后在我們的main中添加如下代碼

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

輸出:

5.99
5.00

您需要使用固定來編寫 5.00,這就是為什么,您的輸出不會用於 5.00。

我添加的一個簡短的參考視頻鏈接很有幫助

#include<stdio.h>
int main()

{

 double d=15.6464545347;

printf("%0.2lf",d);

}

要在小數點后設置固定的 2 位數字,請先使用這些:

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

然后打印你的雙值。

這是一個例子:

#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;
}

您必須將“浮動模式”設置為固定。

float num = 15.839;

// this will output 15.84
std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;

int main() {
    int a;
    long int b;
    char c;
    float d;
    double e;
    cin>>a>>b>>c>>d>>e;
    cout<<a<<"\n"<<b<<"\n"<<c<<"\n";
    cout<<fixed<<setprecision(3)<<d<<"\n";
    cout<<fixed<<setprecision(9)<<e;
    return 0;
}

簡單的代碼肯定會幫助你!

帶模板

#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;
}

與科學類似,也有寬度選項(對列有用)

// 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';
}

從 C++ 20 開始,庫format作為標准引入。 確保您使用的編譯器支持它。

最好使用這個庫來格式化小數。 下面是不同的例子。

#include <format>


//PRINT TWO DECIMALS.
double dVal = 0.12345;
std::cout << std::format("{:.2}", dVal) << std::endl; //0.12

//NUMBER.PRINT THREE DECIMALS (NUMBER + THREE DECIMALS = 4).
double dVal2 = 2.23456;
std::cout << std::format("{:.4}", dVal2) << std::endl; //2.235

//TWO NUMBERS.PRINT FOUR DECIMALS (TWO NUMBERS + FOUR DECIMALS = 6).
double dVal4 = 22.34567;
std::cout << std::format("{:.6}", dVal4) << std::endl; //22.3457

最簡單的方法:

#include <iostream> 
using namespace std;

int main() {
double d = 122.345;

printf(".2%lf",d);
return 0; 
}

只是一個小問題; 將以下內容放在標題中

使用命名空間標准;

然后

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

簡化為

cout << 固定 << setprecision(2) << d;

這是一個使用矩陣的例子。

cout<<setprecision(4)<<fixed<<m[i][j]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM