20 lines
597 B
Swift
20 lines
597 B
Swift
import Foundation
|
|
|
|
public enum TokenFormatter {
|
|
public static func format(_ value: Int) -> String {
|
|
if value >= 100_000_000 {
|
|
return adaptive(Double(value) / 100_000_000) + "亿"
|
|
}
|
|
if value >= 10_000 {
|
|
return adaptive(Double(value) / 10_000) + "万"
|
|
}
|
|
return String(value)
|
|
}
|
|
|
|
private static func adaptive(_ value: Double) -> String {
|
|
let integerDigits = String(Int(abs(value.rounded(.towardZero)))).count
|
|
let digits = integerDigits > 3 ? 1 : 3
|
|
return String(format: "%.\(digits)f", value)
|
|
}
|
|
}
|