जावा: Formatter वर्ग का उपयोग करें। उम्मीद के उपयोग के उदाहरण:
StringBuilder sb = new StringBuilder();
// Send all output to the Appendable object sb
Formatter formatter = new Formatter(sb, Locale.US);
// Explicit argument indices may be used to re-order output.
formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
// -> " d c b a"
// Optional locale as the first argument can be used to get
// locale-specific formatting of numbers. The precision and width can be
// given to round and align the value.
formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
// -> "e = +2,7183"
// The '(' numeric flag may be used to format negative numbers with
// parentheses rather than a minus sign. Group separators are
// automatically inserted.
formatter.format("Amount gained or lost since last statement: $ %(,.2f",
balanceDelta);
// -> "Amount gained or lost since last statement: $ (6,217.58)"
स्रोत
2010-07-09 11:56:26
कृपया एक 'printf'-प्रश्न' सी टैग नहीं करते ++ '। –
@ Space_C0wb0y: C++ में printf का उपयोग करने में कुछ भी गलत नहीं है। एक आदर्श दुनिया में हर कोई अपने सी ++ में आईस्ट्रीम का उपयोग करेगा, लेकिन हम एक आदर्श दुनिया में नहीं रहते हैं। इसके अलावा, कभी-कभी एक स्ट्रिंग को स्वरूपित करना आसान होता है जिस तरह से printf के साथ चाहता है। – George
मेरी राय में सी ++ में 'printf' का उपयोग करने में वास्तव में बहुत गलत है। सी और सी ++ दो पूरी तरह से अलग-अलग भाषाएं हैं, और लोग अलग-अलग धारणाएं करते हैं कि सी ++ में कौन सी सी विशेषताएं ठीक हैं और जो नहीं हैं। अलग-अलग धारणाएं हमेशा परेशानी का कारण बनती हैं। –