Skip to content
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

Closed
iRazvan2745 opened this issue May 16, 2023 · 8 comments · Fixed by #58
Closed

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

iRazvan2745 opened this issue May 16, 2023 · 8 comments · Fixed by #58
Assignees
Labels
Improvements New improvements

Comments

@iRazvan2745
Copy link

iRazvan2745 commented May 16, 2023

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];
    }
@iRazvan2745 iRazvan2745 added the Improvements New improvements label May 16, 2023
@LcyDev
Copy link
Collaborator

LcyDev commented Jul 20, 2023

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
Copy link
Collaborator

LcyDev commented Jul 20, 2023

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
Copy link
Collaborator

LcyDev commented Jul 20, 2023

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
Copy link
Collaborator

LcyDev commented Jul 20, 2023

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

@iRazvan2745
Copy link
Author

Do you mind adding me on discord?
Discord: irazvan2745

@LcyDev
Copy link
Collaborator

LcyDev commented Jul 20, 2023

Done, what for tho?

@iRazvan2745
Copy link
Author

iRazvan2745 commented Jul 20, 2023 via email

@LcyDev
Copy link
Collaborator

LcyDev commented Aug 1, 2023

This feature has been added.

@LcyDev LcyDev closed this as completed Aug 1, 2023
@LcyDev LcyDev changed the title [Feature Request]: when user does /pay <number>b it pays 1 000 000 000 [Feature Request]: Compact representations using metric prefixes Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improvements New improvements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants