[stdlib] KT-55619 provide samples and extend documentation for String.format

This commit is contained in:
Ilya Gorbunov
2023-11-21 22:14:20 +01:00
committed by Space Team
parent b1465fbfb8
commit 28c1049518
2 changed files with 66 additions and 4 deletions
@@ -507,4 +507,42 @@ class Strings {
assertPrints(mixedColor, "brown&blue")
}
@Sample
fun formatStatic() {
// format negative number in parentheses
val negativeNumberInParentheses = String.format("%(d means %1\$d", -31416)
assertPrints(negativeNumberInParentheses, "(31416) means -31416")
}
@Sample
fun formatExtension() {
// format negative number in parentheses
val negativeNumberInParentheses = "%(d means %1\$d".format(-31416)
assertPrints(negativeNumberInParentheses, "(31416) means -31416")
}
@Sample
fun formatWithLocaleStatic() {
// format with German conventions
val withGermanThousandsSeparator = String.format(Locale.GERMANY, "%,d", 12345)
assertPrints(withGermanThousandsSeparator, "12.345")
// format with US conventions
val withUSThousandsSeparator = String.format(Locale.US, "%,d", 12345)
assertPrints(withUSThousandsSeparator, "12,345")
}
@Sample
fun formatWithLocaleExtension() {
// format with German conventions
val withGermanThousandsSeparator = "%,d".format(Locale.GERMANY, 12345)
assertPrints(withGermanThousandsSeparator, "12.345")
// 12.345
// format with US conventions
val withUSThousandsSeparator = "%,d".format(Locale.US, 12345)
assertPrints(withUSThousandsSeparator, "12,345")
}
}