Skip to content

Commit

Permalink
fix: use BigDecimal for double rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
urFate committed Apr 3, 2024
1 parent aa4f4e3 commit 79505df
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/main/java/live/shirabox/core/util/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package live.shirabox.core.util

import java.math.BigDecimal
import java.math.RoundingMode
import java.security.MessageDigest

@OptIn(ExperimentalStdlibApi::class)
Expand All @@ -8,4 +10,6 @@ fun String.md5(): String {
val digest = md.digest(this.toByteArray())
return digest.toHexString()
}
fun Double.round(decimals: Int = 2): Double = "%.${decimals}f".format(this).toDouble()
fun Double.round(decimals: Int = 2): Double {
return BigDecimal(this).setScale(decimals, RoundingMode.HALF_UP).toDouble()
}

0 comments on commit 79505df

Please sign in to comment.