/**
 * Calculate the payment amount to be received by the payee, rounding it to 2 decimal places.
 *
 * @param payee The name of the payee.
 * @param amount The original payment amount.
 * @return The rounded payment amount to be received by the payee.
 *
 * Instructions:
 * - Apply the rounding rule to ensure the payment amount is no more than 2 decimal places.
 * - Use a basic loop syntax to repeat the calculation until the desired precision is achieved.
 *
 * Example:
 * calculatePayment("John", 123.4567) -> 123.46
 */
fun calculatePayment(payee: String, amount: Double): Double {

    val precision = 0.001  // Desired precision for rounding to 2 decimal places
    var roundingIteration = 0
    var roundedAmount = amount.round(2)

    // Loop until the desired precision is achieved
    while (Math.abs(roundedAmount - amount) >= precision) {
        roundingIteration++
        roundedAmount = amount.round(2)
    }

    return roundedAmount
}

fun Double.round(decimals: Int): Double = "%.${decimals}f".format(this).toDouble()
/**
 * Calculate the payment amount to be received by the payee, rounding it to 2 decimal places.
 *
 * @param payee The name of the payee.
 * @param amount The original payment amount.
 * @return The rounded payment amount to be received by the payee.
 *
 * Instructions:
 * - Apply the rounding rule to ensure the payment amount is no more than 2 decimal places.
 * - Use a basic loop syntax to repeat the calculation until the desired precision is achieved.
 *
 * Example:
 * calculatePayment("John", 123.4567) -> 123.46
 */
fun calculatePayment(payee: String, amount: Double): Double {
/**
 * Calculate the payment amount to be received by the payee, rounding it to 2 decimal places.
 *
 * @param payee The name of the payee.
 * @param amount The original payment amount.
 * @return The rounded payment amount to be received by the payee.
 *
 * Instructions:
 * - Apply the rounding rule to ensure the payment amount is no more than 2 decimal places.
 * - Use a basic loop syntax to repeat the calculation until the desired precision is achieved.
 *
 * Example:
 * calculatePayment("John", 123.4567) -> 123.46
 */
fun calculatePayment(payee: String, amount: Double): Double {

    val precision = 0.001  // Desired precision for rounding to 2 decimal places
    var roundingIteration = 0
    var roundedAmount = amount.round(2)

    // Loop until the desired precision is achieved
    while (Math.abs(roundedAmount - amount) >= precision) {
        roundingIteration++
        roundedAmount = amount.round(2)
    }

    return roundedAmount
}

fun Double.round(decimals: Int): Double = "%.${decimals}f".format(this).toDouble()