EncryptSL / LiteEco

Minecraft Economy Plugin works with vault.
19 stars 8 forks source link

[Feature Request]: Compact representations using metric prefixes #52

Closed iRazvan2745 closed 11 months ago

iRazvan2745 commented 1 year ago

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:

    public static String compactNumber(int var1) {
        String[] units = {"T", "K", "M", "B"};
        int unitIndex = 0;

        double value = var1;
        while (value >= 1000 && unitIndex < units.length - 1) {
            value /= 1000;
            unitIndex++;
        }

        BigDecimal numberBigDecimal = new BigDecimal(value);
        numberBigDecimal = numberBigDecimal.setScale(1, RoundingMode.HALF_EVEN);
        String formattedValue = numberBigDecimal.stripTrailingZeros().toPlainString();

        return formattedValue + units[unitIndex];
    }
LcyDev commented 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

LcyDev commented 11 months ago
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]
        }
    }
}
LcyDev commented 11 months ago
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
}
LcyDev commented 11 months ago

I will try to make a pull request with this soon.

iRazvan2745 commented 11 months ago

Do you mind adding me on discord? Discord: irazvan2745

LcyDev commented 11 months ago

Done, what for tho?

iRazvan2745 commented 11 months ago

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: @.***>

LcyDev commented 11 months ago

This feature has been added.