When I use String.format with leading zeros and pr...
# getting-started
d
When I use String.format with leading zeros and precision I lose the leading zeros. Is that normal?
Copy code
fun main() {
    val num = 5.0
    val num0 = String.format(Locale.ENGLISH, "%02.0f", num)
    val num2 = String.format(Locale.ENGLISH, "%02.2f", num)
    println("num0 = $num0, num2 = $num2")
}
This prints: num0 = 05, num2 = 5.00
v
Sure, you say it should be at least 2 characters wide and filled with leading zeroes, but it is already 4 characters wide. What you want is
%05.2f
, as the size is the overall size, not the size before the decimal separator.
d
OK, that was too easy, thank you! 👍
👌 1
👌🏾 1
100 Views