In đúng số thập phân bằng cout


133

Tôi có một danh sách các floatgiá trị và tôi muốn in chúng coutvới 2 chữ số thập phân.

Ví dụ:

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

Tôi có thể làm cái này như thế nào?

( setprecisiondường như không giúp được gì trong việc này.)

Câu trả lời:


195

Với <iomanip>, bạn có thể sử dụng std::fixedstd::setprecision

Đây là một ví dụ

#include <iostream>
#include <iomanip>

int main()
{
    double d = 122.345;

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

Và bạn sẽ nhận được đầu ra

122.34

6
Tại sao bạn sử dụng "std: fixed" trong chương trình?
Vilas Joshi

1
Một tiêu đề hữu ích có thể được xác định cho việc này: #define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x) giúp đơn giản hóa việc sử dụng thành:cout<<FIXED_FLOAT(d)
Udayraj Deshmukh

12
@VilasJoshi, setprecision đặt số chữ số sau số thập phân, nếu có 5 chữ số và chúng tôi sử dụng setprecision (2), chúng tôi sẽ nhận được 2 chữ số, nhưng nếu có 0 chữ số thì nó sẽ không hiển thị, sử dụng cố định chúng tôi sẽ sửa được hiển thị như vậy 5 sẽ được biểu thị là 5,00 không 5
vaibnak

43

Bạn đã ở gần đó, cũng cần sử dụng std :: fixed, tham khảo 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;
}

đầu ra:

0.12
1.23
12.35
123.45
1234.50
12345.00

18

setprecision(n)áp dụng cho toàn bộ số, không phải là phần phân số. Bạn cần sử dụng định dạng điểm cố định để áp dụng định dạng cho phần phân số:setiosflags(ios::fixed)


12

Đơn giản hóa câu trả lời được chấp nhận

Ví dụ đơn giản:

#include <iostream>
#include <iomanip>

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

Và bạn sẽ nhận được đầu ra

122.34

Tài liệu tham khảo:


Điều này làm việc cho tôi: std :: cout << std :: setprecision (2) << std :: fixed << d;
Andrea Girardi

5

Tôi đã có một vấn đề cho số nguyên trong khi muốn định dạng nhất quán.

Viết lại cho đầy đủ:

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

Bạn đang thiếu std :: ngay trước cout và endl vì bạn không sử dụng không gian tên.
blackforest-tom

@ blackforest-tom - Có thể bạn đúng, không thể nhớ lại --- Tôi thường sao chép-dán các chương trình làm việc, vì vậy điều này có thể đã chạy như trong Visual Studio hoặc ở một nơi khác.
Manohar Reddy Poreddy

3

Tôi đã gặp vấn đề tương tự trong một cuộc thi viết mã và đây là cách tôi xử lý nó. Đặt độ chính xác từ 2 đến tất cả các giá trị kép

Đầu tiên thêm tiêu đề để sử dụng setprecision

#include <iomanip>

Sau đó thêm mã sau vào chính của chúng tôi

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

Đầu ra:

5.99
5.00

Bạn cần sử dụng cố định để viết 5,00 đó là lý do tại sao, đầu ra của bạn sẽ không đạt 5,00.

Một liên kết video tham khảo ngắn tôi đang thêm rất hữu ích


2

Bạn phải đặt 'chế độ thả nổi' thành cố định.

float num = 15.839;

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

1

Để đặt 2 chữ số cố định sau dấu thập phân, trước tiên hãy sử dụng các chữ số sau:

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

Sau đó in giá trị kép của bạn.

Đây là một ví dụ:

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

1
#include<stdio.h>
int main()

{

 double d=15.6464545347;

printf("%0.2lf",d);

}

0

với các mẫu

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

cũng tương tự đối với khoa học, với tùy chọn chiều rộng cũng (hữu ích cho các cột)

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

-3

Chỉ là một điểm nhỏ; đặt phần sau vào tiêu đề

sử dụng không gian tên std;

sau đó

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

trở nên đơn giản hóa thành

cout << đã sửa << setprecision (2) << d;


2
Vâng, nó đã trở thành "đơn giản hóa", nhưng điều này được khuyến khích mạnh mẽ. Xin đừng sử dụng using namespace std;vì lợi ích của nó - hiểu lý do tại sao bạn làm như vậy.
Carlos F

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.