package com.laseroptek.raman.utils.ext

import kotlin.math.pow
import kotlin.math.round

fun Double.format(decimalPlaces: Int): String {
    require(decimalPlaces >= 0) { "Decimal places must be non-negative." }

    if (decimalPlaces == 0) {
        return this.toInt().toString() // Handle whole numbers efficiently
    }

    val factor = 10.0.pow(decimalPlaces.toDouble())
    val roundedValue = round(this * factor) / factor
    return roundedValue.toString()
}

fun Int.formatWithThousandComma(): String {
    val result = StringBuilder()
    val size = this.toString().length
    return if (size > 3) {
        for (i in size - 1 downTo 0) {
            result.insert(0, this.toString()[i])
            if ((i != size - 1) && i != 0 && (size - i) % 3 == 0)
                result.insert(0, "\'")
        }
        result.toString()
    } else
        this.toString()
}
