2012-05-14 90 views
0

我想知道如何將stringDWORD轉換爲onstringstream,然後再轉換爲AnsiString字符串操作,轉換後在字符串的開頭顯示0

但是,這並不重要,轉換可能是由intstring我只是想知道我怎樣才能使每個字符串轉換成始終顯示6數字,就像如果我的號碼是57,在string它將是000057

謝謝!

回答

7

使用io manipulatorssetfillsetw

#include <iostream> 
#include <string> 
#include <sstream> 
#include <iomanip> 

int main() 
{ 
    std::ostringstream s; 

    s << std::setfill('0') << std::setw(6) << 154; 

    std::cout << s.str() << "\n"; 

    return 0; 
} 
+0

setfill不是成員 '性病' –

+0

@GuilhermeGarcia,是的,它是。檢查鏈接和這個:http://ideone.com/RSGvj。你需要'#include ' – hmjd

+0

#include 是需要的,謝謝= D和btw沒有必要std ::,我可以做s << setfill('0')<< setw(6) –

0

所以,關於格式化輸出的問題? 可以使用iostream::width和'的iostream ::補「:

// field width 
#include <iostream> 
using namespace std; 

int main() { 
    cout << 100 << endl; 
    cout.width(6); 
    cout.fill('0'); 
    cout << 100 << endl; 
    return 0; 
} 
+0

我不使用cout,但無論如何,hmjd asnwer可能會解決我的問題 –

+1

不管你使用cout還是不同的ostream,它仍然是一個ostream。 –