Raphael TEYSSANDIER
double
string
joseph_ivie
inline fun Double.toStringNoExponential(): String { val preDecimal = toLong().toString() val r = rem(1) if(r == 0.0) return preDecimal val availableDigits = 12 - preDecimal.length val postDecimal = r.times(10.0.pow(availableDigits)).roundToInt() if(postDecimal == 0) return preDecimal else return preDecimal + "." + postDecimal.toString().padStart(availableDigits, '0').trimEnd('0') } fun Double.commaString(): String { val clean = this.toStringNoExponential().filter { it.isDigit() || it == '.' } val preDecimal = clean.substringBefore('.').reversed().chunked(3) { it.reversed() }.reversed().joinToString(",") val postDecimal = clean.substringAfter('.', "") return if (clean.contains('.')) "$preDecimal.$postDecimal" else preDecimal }
A modern programming language that makes developers happier.