Skip to content

Instantly share code, notes, and snippets.

@mutkuensert
Last active September 5, 2023 14:21
Show Gist options
  • Save mutkuensert/57c1196852e8ed9d3b9bfd4485556ef1 to your computer and use it in GitHub Desktop.
Save mutkuensert/57c1196852e8ed9d3b9bfd4485556ef1 to your computer and use it in GitHub Desktop.
Digit remover extension for Double.
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
fun Double.withDecimalDigits(number: Int = 1): Double {
var diesisForDecimals = ""
repeat(number) {
diesisForDecimals += "#"
}
val symbols = DecimalFormatSymbols().apply { decimalSeparator = '.' }
val df = DecimalFormat("#.$diesisForDecimals", symbols)
df.roundingMode = RoundingMode.HALF_EVEN
return df.format(this).toDouble()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment