Skip to content

Instantly share code, notes, and snippets.

@Innovatewithapple
Last active July 26, 2023 07:06
Show Gist options
  • Save Innovatewithapple/002a09e4345a028e31d1621668cd6443 to your computer and use it in GitHub Desktop.
Save Innovatewithapple/002a09e4345a028e31d1621668cd6443 to your computer and use it in GitHub Desktop.
Format Double or Float values without rounded. Use this method and set maximum decimals according to your need.
func IWADoubleFormat(_ a: Double, max: Int) -> Double {
let stringArr = String(a).split(separator: ".")
let decimals = Array(stringArr[1])
var string = "\(stringArr[0])."
var count = 0;
for n in decimals {
if count == max { break }
string += "\(n)"
count += 1
}
let double = Double(string)!
return double
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment