Closed iRazvan2745 closed 11 months ago
It's a nice concept, maybe it could be implemented.
I'll make an improved code in Kotlin and fixing some wrongdoings with the example
object NumberFormatter {
private val multipliersMap =
mapOf('K' to 1000L, 'M' to 1_000_000L, 'B' to 1_000_000_000L, 'T' to 1_000_000_000_000L)
private val units = charArrayOf('K', 'M', 'B', 'T')
fun parseCompactNumber(compactNumber: String): Long {
if (compactNumber.isEmpty()) {
throw IllegalArgumentException("Invalid input")
}
val lastChar = compactNumber.lastOrNull()
if (lastChar == null || !multipliersMap.containsKey(lastChar)) {
return compactNumber.toLongOrNull()
?: throw IllegalArgumentException("Invalid compact representation")
}
val multiplier = multipliersMap[lastChar]!!
val value =
compactNumber.dropLast(1).toDoubleOrNull()
?: throw IllegalArgumentException("Invalid compact representation")
return (value * multiplier).toLong()
}
fun compactNumber(value: Long): String {
if (value < 1000) {
return value.toString()
}
var unitIndex = 0
var result = value.toDouble()
while (result >= 1000 && unitIndex < units.size) {
result /= 1000
unitIndex++
}
val formattedValue = "%.1f".format(result)
return if (formattedValue.endsWith(".0")) {
formattedValue.dropLast(2) + units[unitIndex - 1]
} else {
formattedValue + units[unitIndex - 1]
}
}
}
fun main() {
println(NumberFormatter.compactNumber(123)) // Output: 123
println(NumberFormatter.compactNumber(1_200)) // Output: 1.2K
println(NumberFormatter.compactNumber(1_200_000)) // Output: 1.2M
println(NumberFormatter.compactNumber(1_200_000_000)) // Output: 1.2B
println(NumberFormatter.compactNumber(1_200_000_000_000)) // Output: 1.2T
println()
println(NumberFormatter.compactNumber(12_000)) // Output: 12K
println(NumberFormatter.compactNumber(120_000)) // Output: 120K
println()
println(NumberFormatter.parseCompactNumber("123")) // Output: 123
println(NumberFormatter.parseCompactNumber("1.2K")) // Output: 1200
println(NumberFormatter.parseCompactNumber("1.2M")) // Output: 1200000
println(NumberFormatter.parseCompactNumber("1.2B")) // Output: 1200000000
println(NumberFormatter.parseCompactNumber("1.2T")) // Output: 1200000000000
}
I will try to make a pull request with this soon.
Do you mind adding me on discord? Discord: irazvan2745
Done, what for tho?
Wait,have you added the feature THIS FAST wow
On Thu, Jul 20, 2023, 21:27 Lucyy_ @.***> wrote:
Done, what for tho?
— Reply to this email directly, view it on GitHub https://github.com/EncryptSL/LiteEco/issues/52#issuecomment-1644395134, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASJX4QCQX6LH57MWIHGDZ5TXRFZ7NANCNFSM6AAAAAAYEFMN2A . You are receiving this because you authored the thread.Message ID: @.***>
This feature has been added.
Type of function to add or improvement
when user does /payb it pays 1 000 000 000
Your ideas
Someone made code for the feature and I was wondering if you could implement it în your plugin LiteEco code: