-
Are you sure you shouldn't be using
Decimal? It sounds like you're maybe doing whatDecimalis for.
Swift has unit tests that look like this:
@Test func test() {
#expect(convertAmount("0.29") == 29)
#expect(convertAmount("0.58") == 58)
#expect(convertAmount("16.08") == 16_08)
#expect(convertAmount("1025.12") == 1025_12)
}
If you're using Int64, I don't know that NSDecimalNumber is available to you. If it is…
func convertAmount(_ amount: String?) -> Int64 {
guard let decimal = amount.flatMap({ Decimal(string: $0) })
else { return 0 }
return .init(truncating: decimal * 100 as NSDecimalNumber)
}
If not,
func convertAmount(_ amount: String?) -> Int64 {
guard let double = amount.flatMap(Double.init)
else { return 0 }
return .init((double * 100).rounded())
}