DecimalFormatter

Number.prototype.toFixed, toExponential and toPrecision.

Upstream leans on java.math.BigDecimal for the exact decimal expansion of a double and its HALF_UP rounding. Common Kotlin has no BigDecimal, so this file expands the double by hand (every double is a finite decimal) and rounds the digit string itself. Same results, same shapes.

Functions

Link copied to clipboard
fun toExponential(v: Double, fractionDigits: Int): String
Link copied to clipboard
fun toFixed(v: Double, fractionDigits: Int): String
Link copied to clipboard
fun toPrecision(v: Double, precision: Int): String