![](/img/trans.png)
[英]C++ Reading fixed number of lines of integers with unknown number of integers in each line
[英]Printing integers with a fixed number of zeros in C++
問題很簡單。 我搞砸了std::fixed 、 std::setprecicion 、 std::setw ,但它們都沒有解決我的問題。
如果我有一個 integer 變量等於 1,我希望它打印為: 1000
謝謝你。
我想你的意思是你希望它打印為 0001,而不是 1000。
查看帶有格式字符串的 printf / sprintf。 例如:
#include <iostream>
int main(int argc, char **argv) {
int i = 1;
printf("%04d\n", i);
}
關於您要達到的目標,這個問題不是很清楚。
如果要打印附加了 3 個零的變量的值,則應該只打印變量,然后打印 3 個零。
cout << x << "000" << endl;
如果你需要用它做任何計算,你總是可以把它乘以1000
。
int y = x * 1000;
cout << y << endl;
請記住,這可能會導致溢出,但是...
您可以將std::left
與std::setw
和std::setfill
一起使用以在右側添加許多特定的填充字符。 std::cout
示例:
#include <iostream>
#include <iomanip>
#include <ios>
int main() {
std::cout << std::setw(4) << std::setfill('0') << std::left << 1;
return 0;
}
我相信它應該完成這項工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.