2021-01-06 20:56:06 +00:00
|
|
|
|
|
|
|
function exponentialFormat(num, precision, mantissa = true) {
|
|
|
|
let e = num.log10().floor()
|
|
|
|
let m = num.div(Decimal.pow(10, e))
|
|
|
|
if (m.toStringWithDecimalPlaces(precision) == 10) {
|
2021-05-11 00:19:29 +00:00
|
|
|
m = decimalOne
|
2021-01-06 20:56:06 +00:00
|
|
|
e = e.add(1)
|
|
|
|
}
|
2021-04-07 18:27:40 +00:00
|
|
|
e = (e.gte(1e9) ? format(e, 1) : (e.gte(10000) ? commaFormat(e, 0) : e.toStringWithDecimalPlaces(0)))
|
2021-01-06 20:56:06 +00:00
|
|
|
if (mantissa)
|
|
|
|
return m.toStringWithDecimalPlaces(precision) + "e" + e
|
|
|
|
else return "e" + e
|
|
|
|
}
|
|
|
|
|
|
|
|
function commaFormat(num, precision) {
|
|
|
|
if (num === null || num === undefined) return "NaN"
|
|
|
|
if (num.mag < 0.001) return (0).toFixed(precision)
|
2021-05-13 03:21:39 +00:00
|
|
|
let init = num.toStringWithDecimalPlaces(precision)
|
|
|
|
let portions = init.split(".")
|
|
|
|
portions[0] = portions[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
|
|
|
|
if (portions.length == 1) return portions[0]
|
|
|
|
return portions[0] + "." + portions[1]
|
2021-01-06 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function regularFormat(num, precision) {
|
|
|
|
if (num === null || num === undefined) return "NaN"
|
2021-05-10 22:43:04 +00:00
|
|
|
if (num.mag < 0.0001) return (0).toFixed(precision)
|
2021-05-13 03:21:39 +00:00
|
|
|
if (num.mag < 0.1 && precision !==0) precision = Math.max(precision, 4)
|
2021-01-06 20:56:06 +00:00
|
|
|
return num.toStringWithDecimalPlaces(precision)
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixValue(x, y = 0) {
|
|
|
|
return x || new Decimal(y)
|
|
|
|
}
|
|
|
|
|
|
|
|
function sumValues(x) {
|
|
|
|
x = Object.values(x)
|
2021-05-11 00:19:29 +00:00
|
|
|
if (!x[0]) return decimalZero
|
2021-01-06 20:56:06 +00:00
|
|
|
return x.reduce((a, b) => Decimal.add(a, b))
|
|
|
|
}
|
|
|
|
|
2021-05-06 21:51:06 +00:00
|
|
|
function format(decimal, precision = 2, small) {
|
|
|
|
small = small || modInfo.allowSmall
|
2021-01-06 20:56:06 +00:00
|
|
|
decimal = new Decimal(decimal)
|
|
|
|
if (isNaN(decimal.sign) || isNaN(decimal.layer) || isNaN(decimal.mag)) {
|
|
|
|
player.hasNaN = true;
|
|
|
|
return "NaN"
|
|
|
|
}
|
|
|
|
if (decimal.sign < 0) return "-" + format(decimal.neg(), precision)
|
|
|
|
if (decimal.mag == Number.POSITIVE_INFINITY) return "Infinity"
|
|
|
|
if (decimal.gte("eeee1000")) {
|
|
|
|
var slog = decimal.slog()
|
|
|
|
if (slog.gte(1e6)) return "F" + format(slog.floor())
|
|
|
|
else return Decimal.pow(10, slog.sub(slog.floor())).toStringWithDecimalPlaces(3) + "F" + commaFormat(slog.floor(), 0)
|
|
|
|
}
|
|
|
|
else if (decimal.gte("1e100000")) return exponentialFormat(decimal, 0, false)
|
|
|
|
else if (decimal.gte("1e1000")) return exponentialFormat(decimal, 0)
|
|
|
|
else if (decimal.gte(1e9)) return exponentialFormat(decimal, precision)
|
|
|
|
else if (decimal.gte(1e3)) return commaFormat(decimal, 0)
|
2021-05-10 22:43:04 +00:00
|
|
|
else if (decimal.gte(0.0001) || !small) return regularFormat(decimal, precision)
|
2021-05-06 21:51:06 +00:00
|
|
|
else if (decimal.eq(0)) return (0).toFixed(precision)
|
|
|
|
|
|
|
|
decimal = invertOOM(decimal)
|
|
|
|
let val = ""
|
2021-05-07 22:53:49 +00:00
|
|
|
if (decimal.lt("1e1000")){
|
2021-05-06 21:51:06 +00:00
|
|
|
val = exponentialFormat(decimal, precision)
|
2021-05-07 22:53:49 +00:00
|
|
|
return val.replace(/([^(?:e|F)]*)$/, '-$1')
|
2021-05-06 21:51:06 +00:00
|
|
|
}
|
|
|
|
else
|
2021-05-07 22:53:49 +00:00
|
|
|
return format(decimal, precision) + "⁻¹"
|
2021-05-06 21:51:06 +00:00
|
|
|
|
2021-01-06 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatWhole(decimal) {
|
|
|
|
decimal = new Decimal(decimal)
|
|
|
|
if (decimal.gte(1e9)) return format(decimal, 2)
|
|
|
|
if (decimal.lte(0.98) && !decimal.eq(0)) return format(decimal, 2)
|
|
|
|
return format(decimal, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTime(s) {
|
|
|
|
if (s < 60) return format(s) + "s"
|
|
|
|
else if (s < 3600) return formatWhole(Math.floor(s / 60)) + "m " + format(s % 60) + "s"
|
2021-04-20 14:44:40 +00:00
|
|
|
else if (s < 86400) return formatWhole(Math.floor(s / 3600)) + "h " + formatWhole(Math.floor(s / 60) % 60) + "m " + format(s % 60) + "s"
|
|
|
|
else if (s < 31536000) return formatWhole(Math.floor(s / 86400) % 365) + "d " + formatWhole(Math.floor(s / 3600) % 24) + "h " + formatWhole(Math.floor(s / 60) % 60) + "m " + format(s % 60) + "s"
|
2021-04-29 23:15:23 +00:00
|
|
|
else return formatWhole(Math.floor(s / 31536000)) + "y " + formatWhole(Math.floor(s / 86400) % 365) + "d " + formatWhole(Math.floor(s / 3600) % 24) + "h " + formatWhole(Math.floor(s / 60) % 60) + "m " + format(s % 60) + "s"
|
2021-01-06 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toPlaces(x, precision, maxAccepted) {
|
|
|
|
x = new Decimal(x)
|
|
|
|
let result = x.toStringWithDecimalPlaces(precision)
|
|
|
|
if (new Decimal(result).gte(maxAccepted)) {
|
|
|
|
result = new Decimal(maxAccepted - Math.pow(0.1, precision)).toStringWithDecimalPlaces(precision)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2021-05-06 21:51:06 +00:00
|
|
|
|
|
|
|
// Will also display very small numbers
|
|
|
|
function formatSmall(x, precision=2) {
|
|
|
|
return format(x, precision, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
function invertOOM(x){
|
|
|
|
let e = x.log10().ceil()
|
|
|
|
let m = x.div(Decimal.pow(10, e))
|
|
|
|
e = e.neg()
|
|
|
|
x = new Decimal(10).pow(e).times(m)
|
|
|
|
|
|
|
|
return x
|
|
|
|
}
|