Skip to content

Commit

Permalink
Update NumberKtx.kt (#960)
Browse files Browse the repository at this point in the history
**Background**

Simplified

**Changes**

Shortened the code

**Test plan**

Eye test
  • Loading branch information
GeorgCantor authored Sep 23, 2024
1 parent d5a8ab0 commit d9eed2f
Showing 1 changed file with 2 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,11 @@ private const val PERCENT_MAX = 100
* If not, return Int.MIN_VALUE or Int.MAX_VALUE
*/
fun Long.toIntSafe(): Int {
if (this > Int.MAX_VALUE) {
return Int.MAX_VALUE
}

if (this < Int.MIN_VALUE) {
return Int.MIN_VALUE
}

return this.toInt()
return coerceIn(Int.MIN_VALUE.toLong(), Int.MAX_VALUE.toLong()).toInt()
}

fun Float.roundPercentToString(): String {
val processedPercent = if (this > 1.0f) 1.0f else if (this < 0.0f) 0.0f else this
val processedPercent = coerceIn(0.0f, 1.0f)
return "${(processedPercent * PERCENT_MAX).roundToInt()}%"
}

Expand Down

0 comments on commit d9eed2f

Please sign in to comment.