I have a problem to sort some numbers, which are string first. The numbers are too huge for UInt64, so i converted the string numbers to float and then sorted it. That works out great. But then i need to print these numbers with no decimals. So I tried to format the numbers. But the Bigger number are changing its value after formatting it.
Here is the Input array to sort -
["6","31415926535897932384626433832795","1","3","10","3","5"]
And I need to print output in exactly this format -
1
3
3
5
10
31415926535897932384626433832795
Here is my code in swift -
import Foundation
var a = Array<Float>()
var b = ["6","31415926535897932384626433832795","1","3","10","3","5"]
a = b.map{ Float($0)! }
for i in 0..<(a.count-1){
var min = i
for j in (i+1)..<a.count{
if a[j] < a[min] {
min = j
}
}
var temp = a[i]
a[i] = a[min]
a[min] = temp
}
for val in a{
print(String(format: "%.0f",val.rounded(.down)))
}
My Output is -
1
3
3
5
6
10
31415927314585224784361549725696
If you notice the last biggest number is changed from the original input. Any suggestions would be much appreciated! Thanks!