toPercentage

fun Int.toPercentage(total: Int): Float

Calculates the percentage that this number represents out of a given total.

This function computes (this / total) * 100. It handles the case where the total is zero by returning 0f to prevent division-by-zero errors. The result is returned as a Float to preserve decimal precision.

Example:

val score = 40
val totalPossible = 50
val percentage = score.toPercentage(totalPossible) // Result: 80.0f

val items = 0
val totalItems = 0
val zeroPercentage = items.toPercentage(totalItems) // Result: 0.0f

Return

The calculated percentage as a Float. Returns 0f if total is 0.

Parameters

total

The total value to calculate the percentage against. This is the denominator.