This commit is contained in:
InsanusMokrassar 2023-03-07 22:28:56 +06:00
parent 88ee82e1c6
commit 2c7fd320eb
4 changed files with 23 additions and 0 deletions

View File

@ -3,6 +3,7 @@
## 0.17.3 ## 0.17.3
* `Common`: * `Common`:
* Add `fixed` extensions for `Float` and `Double`
* New function `emptyDiff` * New function `emptyDiff`
* Now you may pass custom `comparisonFun` to all `diff` functions * Now you may pass custom `comparisonFun` to all `diff` functions

View File

@ -0,0 +1,6 @@
package dev.inmo.micro_utils.common
val FixedSignsRange = 0 .. 100
expect fun Float.fixed(signs: Int): Float
expect fun Double.fixed(signs: Int): Double

View File

@ -0,0 +1,4 @@
package dev.inmo.micro_utils.common
actual fun Float.fixed(signs: Int): Float = this.asDynamic().toFixed(signs.coerceIn(FixedSignsRange)).unsafeCast<String>().toFloat()
actual fun Double.fixed(signs: Int): Double = this.asDynamic().toFixed(signs.coerceIn(FixedSignsRange)).unsafeCast<String>().toDouble()

View File

@ -0,0 +1,12 @@
package dev.inmo.micro_utils.common
import java.math.BigDecimal
import java.math.RoundingMode
actual fun Float.fixed(signs: Int): Float = BigDecimal.valueOf(this.toDouble())
.setScale(signs.coerceIn(FixedSignsRange), RoundingMode.HALF_UP)
.toFloat();
actual fun Double.fixed(signs: Int): Double = BigDecimal.valueOf(this)
.setScale(signs.coerceIn(FixedSignsRange), RoundingMode.HALF_UP)
.toDouble();