2013-10-01 51 views
4
Formatter f = new Formatter(new StringBuffer()); 
f.format("%06d",11434235); 
System.out.println(f); 

版畫的價值11434235如何限制擴大

有沒有辦法從擴大輸出限制Formatter的格式化?

也就是說,輸出應該是114342而不是11434235當格式字符串是"%06d"

+3

你的同學? http://stackoverflow.com/questions/19110103/string-format-get-maximum-value/19110210#19110210 –

+0

@ user2310289:我不是學生隊友.. :)我發佈了它的好奇心... –

+0

哦,我看,使用相同的值'11434235'困惑我。 –

回答

3

你可以簡單地格式化此爲String,所以我想用format()最好的解決辦法將是

Formatter f = new Formatter(new StringBuffer()); 
f.format("%.6s",11434235); 
2

簡單的解決方法:

Formatter f = new Formatter(new StringBuffer()); 
f.format("%06d", 11434235); 
System.out.println(f.toString().substring(0, 6)); 

見鰻魚李某對與只是一個格式字符串解決方案。

+1

好的方法隊友,只是有一個疑問,爲什麼我們需要在你的代碼格式聲明,因爲'substring(0,6)'將提供解決方案嗎? – Woody

+1

即使對於較小的數字,該格式也會將其吹至六位數。 – Thilo

+0

那麼,實際上可以指定這樣一個剪輯,檢查我的答案。 –