Repository: Automattic/pocket-casts-android
File: modules/features/account/src/main/java/au/com/shiftyjelly/pocketcasts/account/onboarding/components/SubscriptionPlanRow.kt (Kotlin)

Code before:
        ) {
            Image(
                painter = painterResource(IR.drawable.ic_checkmark_small),
                colorFilter = ColorFilter.tint(MaterialTheme.theme.colors.primaryUi01),
                contentDescription = null,
                modifier = Modifier
                    .fillMaxSize()
                    .padding(5.dp),
            )
        }
    } else {
        Box(
            modifier = modifier.border(
                width = 1.dp,
                color = MaterialTheme.theme.colors.primaryIcon02,
                shape = CircleShape,
            ),
        )
    }
}

private val SubscriptionPlan.pricePerMonth: Float
    get() {
        val pricePerMonth = when (billingCycle) {
            BillingCycle.Monthly -> recurringPrice?.amount?.toFloat() ?: 0f
            BillingCycle.Yearly -> (recurringPrice?.amount?.toFloat() ?: 0f) / 12f
        }
        return pricePerMonth
    }

private val SubscriptionPlan.recurringPrice: Price? get() = when (this) {
    is SubscriptionPlan.Base -> pricingPhase.price
    is SubscriptionPlan.WithOffer -> pricingPhases.find { it.schedule.recurrenceMode is PricingSchedule.RecurrenceMode.Infinite }?.price
}

private val SubscriptionPlan.pricePerWeek: Float
    get() {
        val pricePerWeek = when (billingCycle) {
            BillingCycle.Monthly -> (recurringPrice?.amount?.toFloat() ?: 0f) / 4f
            BillingCycle.Yearly -> (recurringPrice?.amount?.toFloat() ?: 0f) / 52f
        }
        return pricePerWeek
    }

@Composable
private fun SubscriptionPlan.pricePerPeriod(config: RowConfig): String? {
    return if (this.billingCycle == BillingCycle.Yearly) {
        when (config.pricePerPeriod) {
            PricePerPeriod.PRICE_PER_MONTH -> {
                val currencyCode = recurringPrice?.currencyCode
                if (currencyCode == "USD") {

Review comment (suggestion):
⚠️ When dealing with prices we should do all the manipulations using `BigDecimal` functions. Conversion to `Float` should be done only at the end for the UI purposes.

It doesn't matter much in this instance but it is a bad practice to use floating-point arithmetic when dealing with money as it can lead to rounding errors.

Code after:
                painter = painterResource(IR.drawable.ic_checkmark_small),
                colorFilter = ColorFilter.tint(MaterialTheme.theme.colors.primaryUi01),
                contentDescription = null,
                modifier = Modifier
                    .fillMaxSize()
                    .padding(5.dp),
            )
        }
    } else {
        Box(
            modifier = modifier.border(
                width = 1.dp,
                color = MaterialTheme.theme.colors.primaryIcon02,
                shape = CircleShape,
            ),
        )
    }
}

private val SubscriptionPlan.pricePerMonth: Float
    get() {
        val pricePerMonth = when (billingCycle) {
            BillingCycle.Monthly -> recurringPrice.amount
            BillingCycle.Yearly -> recurringPrice.amount / monthsInYear
        }
        return pricePerMonth.toFloat()
    }

private val SubscriptionPlan.pricePerWeek: Float
    get() {
        val pricePerWeek = when (billingCycle) {
            BillingCycle.Monthly -> recurringPrice.amount * monthsInYear
            BillingCycle.Yearly -> recurringPrice.amount
        } / weeksInYear
        return pricePerWeek.toFloat()
    }

private val monthsInYear = 12.toBigDecimal()
private val weeksInYear = 52.toBigDecimal()

@Composable
private fun SubscriptionPlan.pricePerPeriod(config: RowConfig): String? {
    return if (this.billingCycle == BillingCycle.Yearly) {
        when (config.pricePerPeriod) {
            PricePerPeriod.PRICE_PER_MONTH -> {
                val currencyCode = recurringPrice.currencyCode
                if (currencyCode == "USD") {
                    stringResource(LR.string.price_per_month_usd, pricePerMonth)
                } else {
                    stringResource(LR.string.price_per_month, pricePerMonth, currencyCode)
                }