|
1 |
| - |
2 |
| - |
3 | 1 | package moe.nea.firmament.util
|
4 | 2 |
|
5 | 3 | import com.google.common.math.IntMath.pow
|
6 | 4 | import kotlin.math.absoluteValue
|
7 | 5 | import kotlin.time.Duration
|
| 6 | +import kotlin.time.Duration.Companion.seconds |
8 | 7 |
|
9 | 8 | object FirmFormatters {
|
10 |
| - fun formatCommas(int: Int, segments: Int = 3): String = formatCommas(int.toLong(), segments) |
11 |
| - fun formatCommas(long: Long, segments: Int = 3): String { |
12 |
| - val α = long / 1000 |
13 |
| - if (α != 0L) { |
14 |
| - return formatCommas(α, segments) + "," + (long - α * 1000).toString().padStart(3, '0') |
15 |
| - } |
16 |
| - return long.toString() |
17 |
| - } |
| 9 | + fun formatCommas(int: Int, segments: Int = 3): String = formatCommas(int.toLong(), segments) |
| 10 | + fun formatCommas(long: Long, segments: Int = 3): String { |
| 11 | + val α = long / 1000 |
| 12 | + if (α != 0L) { |
| 13 | + return formatCommas(α, segments) + "," + (long - α * 1000).toString().padStart(3, '0') |
| 14 | + } |
| 15 | + return long.toString() |
| 16 | + } |
18 | 17 |
|
19 |
| - fun formatCommas(float: Float, fractionalDigits: Int): String = formatCommas(float.toDouble(), fractionalDigits) |
20 |
| - fun formatCommas(double: Double, fractionalDigits: Int): String { |
21 |
| - val long = double.toLong() |
22 |
| - val δ = (double - long).absoluteValue |
23 |
| - val μ = pow(10, fractionalDigits) |
24 |
| - val digits = (μ * δ).toInt().toString().padStart(fractionalDigits, '0').trimEnd('0') |
25 |
| - return formatCommas(long) + (if (digits.isEmpty()) "" else ".$digits") |
26 |
| - } |
| 18 | + fun formatCommas(float: Float, fractionalDigits: Int): String = formatCommas(float.toDouble(), fractionalDigits) |
| 19 | + fun formatCommas(double: Double, fractionalDigits: Int): String { |
| 20 | + val long = double.toLong() |
| 21 | + val δ = (double - long).absoluteValue |
| 22 | + val μ = pow(10, fractionalDigits) |
| 23 | + val digits = (μ * δ).toInt().toString().padStart(fractionalDigits, '0').trimEnd('0') |
| 24 | + return formatCommas(long) + (if (digits.isEmpty()) "" else ".$digits") |
| 25 | + } |
27 | 26 |
|
28 |
| - fun formatDistance(distance: Double): String { |
29 |
| - if (distance < 10) |
30 |
| - return "%.1fm".format(distance) |
31 |
| - return "%dm".format(distance.toInt()) |
32 |
| - } |
| 27 | + fun formatDistance(distance: Double): String { |
| 28 | + if (distance < 10) |
| 29 | + return "%.1fm".format(distance) |
| 30 | + return "%dm".format(distance.toInt()) |
| 31 | + } |
33 | 32 |
|
34 |
| - fun formatTimespan(duration: Duration, millis: Boolean = false): String { |
35 |
| - if (duration.isInfinite()) { |
36 |
| - return if (duration.isPositive()) "∞" |
37 |
| - else "-∞" |
38 |
| - } |
39 |
| - val sb = StringBuilder() |
40 |
| - if (duration.isNegative()) sb.append("-") |
41 |
| - duration.toComponents { days, hours, minutes, seconds, nanoseconds -> |
42 |
| - if (days > 0) { |
43 |
| - sb.append(days).append("d") |
44 |
| - } |
45 |
| - if (hours > 0) { |
46 |
| - sb.append(hours).append("h") |
47 |
| - } |
48 |
| - if (minutes > 0) { |
49 |
| - sb.append(minutes).append("m") |
50 |
| - } |
51 |
| - sb.append(seconds).append("s") |
52 |
| - if (millis) { |
53 |
| - sb.append(nanoseconds / 1_000_000).append("ms") |
54 |
| - } |
55 |
| - } |
56 |
| - return sb.toString() |
57 |
| - } |
| 33 | + fun formatTimespan(duration: Duration, millis: Boolean = false): String { |
| 34 | + if (duration.isInfinite()) { |
| 35 | + return if (duration.isPositive()) "∞" |
| 36 | + else "-∞" |
| 37 | + } |
| 38 | + val sb = StringBuilder() |
| 39 | + if (duration.isNegative()) sb.append("-") |
| 40 | + duration.toComponents { days, hours, minutes, seconds, nanoseconds -> |
| 41 | + if (days > 0) { |
| 42 | + sb.append(days).append("d") |
| 43 | + } |
| 44 | + if (hours > 0) { |
| 45 | + sb.append(hours).append("h") |
| 46 | + } |
| 47 | + if (minutes > 0) { |
| 48 | + sb.append(minutes).append("m") |
| 49 | + } |
| 50 | + val milliTime = nanoseconds / 1_000_000 |
| 51 | + val deciseconds = milliTime / 100 |
| 52 | + if (millis) { |
| 53 | + sb.append(seconds).append("s") |
| 54 | + sb.append(milliTime).append("ms") |
| 55 | + } else if (duration.absoluteValue < 5.seconds && deciseconds != 0) { |
| 56 | + sb.append(seconds).append('.').append(deciseconds.digitToChar()).append("s") |
| 57 | + } else { |
| 58 | + sb.append(seconds).append("s") |
| 59 | + } |
| 60 | + Unit |
| 61 | + } |
| 62 | + return sb.toString() |
| 63 | + } |
58 | 64 |
|
59 | 65 | }
|
0 commit comments