[英]c++ floating point only floats some numbers
當我測試我的代碼時,它不會在我的數字上加上任何小數。 然而,在我的第二次測試中,除了一個數字之外,所有數字都得到小數。
我不確定是否只是浮點數僅在用戶需要小數時才輸入小數,或者我的代碼中是否缺少某些內容。 我是否需要指定要包含多少個小數,如果需要,我將如何更改?
提前感謝您的幫助。
int main()
{
float income;
cout << "\tYour monthly income: ";
cin >> income;
cout << "\tItem Budget Actual\n";
cout << "\t=============== =============== ===============\n";
cout << "\tIncome" << setw(11) << right << "$" << setw(11) << right << income;
cout << setw(5) << right << "$" << setw(11) << right << income <<"\n";
}
這是我得到的錯誤
> Your monthly income: 1
> \tIncome $ 1 $ 1\n
Expected: \tIncome $ 1.00 $ 1.00\n
使用std::cout.precision
和std::fixed
到 output 貨幣:
#include <iostream>
// ...
cout.precision(2);
cout << std::fixed;
cout << "\tIncome" << setw(11) << right << "$" << setw(11) << right << income;
cout << setw(5) << right << "$" << setw(11) << right << income <<"\n";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.