format

fun <T : Number, Comparable<T>> T.format(nDecimal: Int = 2): String

Formats a number (Float or Double) to a string with specified number of decimal places. Handles special cases like NaN and Infinity. Uses locale-specific decimal separator. Removes trailing zeros after decimal point.

Return

Formatted string representation of the number

Examples: 123.4567.format() -> "123.46" 123.4f.format() -> "123.40" 123.0.format() -> "123" (-123.45).format() -> "-123.45" Double.NaN.format() -> "NaN" Float.POSITIVE_INFINITY.format() -> "∞"

Parameters

nDecimal

Number of decimal places (default is 2)