Last active
September 5, 2023 14:21
-
-
Save mutkuensert/57c1196852e8ed9d3b9bfd4485556ef1 to your computer and use it in GitHub Desktop.
Digit remover extension for Double.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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