-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request]: Compact representations using metric prefixes #52
Labels
Improvements
New improvements
Comments
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? |
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
<#52 (comment)>,
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Type of function to add or improvement
when user does /pay b 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:
The text was updated successfully, but these errors were encountered: