Currently working on a simple function which does a great job for me.
For example: If I have 1000, It'll print out 1.0K, or 1,000,000 it'll be 1M. Everything works fine until here.
What if I wanted to turn 1,000,000,000 into 1B?
I tried the following:
func formatPoints(from: Int) -> String {
let number = Double(from)
let thousand = number / 1000
let million = number / 1000000
let billion = number / 1000000000
if million >= 1.0 {
return "\(round(million*10)/10)M"
} else if thousand >= 1.0 {
return "\(round(thousand*10)/10)K"
} else if billion >= 1.0 {
return ("\(round(billion*10/10))B")
} else {
return "\(Int(number))"}
}
print(formatPoints(from: 1000000000))
But it returns 1000.0M
, not 1B.