sprintf का उपयोग
sprintf एक text template में blanks को आपकी values से भरता है, एक form letter की तरह जहाँ हर blank सही नाम से stamp होता है। यह decimals जैसी चीज़ें भी control करता है।
In this page:
Syntax
sprintf("format %s %d %.2f", string_value, integer_value, decimal_value)
sprintf
sprintf() C-style format specifiers इस्तेमाल करते हुए एक string में values format करता है, जैसे strings के लिए %s, integers के लिए %d, और fixed decimal places के लिए %.2f।
यह formatting पर precise control देता है जो paste() offer नहीं करता, जैसे zero-padding या fixed-width fields। यह कई दूसरी languages में मिलने वाली printf-style formatting का R equivalent है।
Note:
sprintf("%.2f", x) display के लिए एक number को exactly 2 decimal places तक format करने का standard तरीका है।
उदाहरण: sprintf
name <- "Alice"
score <- 92.5
cat(sprintf("Name: %s, Score: %.1f%%\n", name, score))
cat(sprintf("Padded number: %05d\n", 42))
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
92.5जैसे एक decimal number के साथ%dलिखना, जो एक error देता है;%fया%.1fइस्तेमाल करें।- literal percent sign के लिए
%%भूल जाना, इसलिए format टूट जाता है। - format specifiers से कम values pass करना, जो
too few argumentsदेता है।
चैप्टर सारांश
paste(),paste0(),nchar(),substr(), और case functions strings बनाते और inspect करते हैं।gsub(),sub(), औरstrsplit()text replace और split करते हैं।sprintf()values को strings में format करता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: