Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
Created November 23, 2021 06:17
Show Gist options
  • Save EmmanuelGuther/fe84750c9b2b748a134929bbbe556e8e to your computer and use it in GitHub Desktop.
Save EmmanuelGuther/fe84750c9b2b748a134929bbbe556e8e to your computer and use it in GitHub Desktop.
// Test if number have not numFractionDigits ex: (40.12345).roundTo(7) cant get the last 7 because have 5
private fun Double.maxDecimals(numFractionDigits: Int) = try {
val factor = 10.0.pow(numFractionDigits.toDouble())
(this * factor).roundToInt() / factor
} catch (e: Exception) {
e.localizedMessage?.toString()?.let { Log.e("Double.maxDecimals", it) }
this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment