Hassaan
10/17/2023, 9:17 AMval double1 = 1234.5678
println(String.format("%f", double1)) //1234.567800
println(String.format("%f", 1.2)) //1.200000
println(String.format("%f", 2.445)) //2.445000
what if we don’t need trailing zeroes? how would you approach?Sam
10/17/2023, 9:30 AMDecimalFormat
instead of String.format
to get access to more formatting options.
val formatter = DecimalFormat("#.######")
println(formatter.format(1234.5678)) // 1234.5678
println(formatter.format(1.2)) // 1.2
println(formatter.format(2.445)) // 2.445
The patterns are different from the string format ones, so look at the docs to see what options you have.Vishnu Shrikar
10/17/2023, 3:00 PMKlitos Kyriacou
10/17/2023, 3:26 PMDouble.toString()
already does what you want, so just do println(number)
or, if you really need it to be inside a String.format
you can use String.format("%s", number)
(or preferably "%s".format(number)
). If that doesn't do what you want, describe the problem in more detail.