9
0
mirror of https://github.com/Auxilor/EcoBits.git synced 2026-01-06 15:52:04 +00:00

Updated format

This commit is contained in:
Auxilor
2022-11-21 20:05:36 +00:00
parent 616a900e3c
commit 252e1334e8

View File

@@ -13,6 +13,10 @@ import com.willfp.eco.core.placeholder.PlayerlessPlaceholder
import com.willfp.eco.core.price.Prices
import com.willfp.eco.util.toNiceString
import org.bukkit.OfflinePlayer
import java.text.DecimalFormat
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.pow
class Currency(
val id: String,
@@ -40,7 +44,7 @@ class Currency(
plugin,
id
) {
it.getBalance(this).toString()
it.getBalance(this).toNiceString()
}
)
@@ -49,7 +53,7 @@ class Currency(
plugin,
"${id}_formatted"
) {
it.getBalance(this).toNiceString()
it.getBalance(this).formatWithExtension()
}
)
@@ -75,6 +79,20 @@ class Currency(
}
}
fun Double.formatWithExtension(): String {
val suffix = charArrayOf(' ', 'k', 'M', 'B', 'T', 'P', 'E')
val numValue = this.toLong()
val value = floor(log10(numValue.toDouble())).toInt()
val base = value / 3
return if (value >= 3 && base < suffix.size) {
DecimalFormat("#0.0").format(numValue / 10.0.pow((base * 3).toDouble())) + suffix[base]
} else {
DecimalFormat("#,##0").format(numValue)
}
}
fun OfflinePlayer.getBalance(currency: Currency): Double {
return this.profile.read(currency.key)
}