Skip to content

Instantly share code, notes, and snippets.

@codewithsanthoshofficial
Last active March 17, 2021 05:55
Show Gist options
  • Select an option

  • Save codewithsanthoshofficial/5b49a868921462857e8c9111d570aab9 to your computer and use it in GitHub Desktop.

Select an option

Save codewithsanthoshofficial/5b49a868921462857e8c9111d570aab9 to your computer and use it in GitHub Desktop.
DecimalsHandleSwift5KSK
extension Constant {
static func twoDecimalValueReturnDouble(_ total:Double) -> Double {
let val = String(format: "%.2f", total)
let final = (val as NSString).doubleValue
return final
}
static func twoDecimalValue(_ total:Double) -> String {
return String(format: "%.2f", total)
}
static func convertStringToDouble(_ total:String) -> Double {
let value = (total as NSString).doubleValue
return value
}
static func singleOrRemoveDecimalValueString(_ total:String) -> String {
let myFloat = (total as NSString).floatValue
let intValue = Int(myFloat)
if Float(intValue) < myFloat {
return String(format: "%.2f", myFloat)
}
return String(format: "%.f", myFloat)
}
static func twoDecimalValueString(_ total:String) -> String {
let myFloat = (total as NSString).floatValue
return String(format: "%.2f", myFloat)
}
static func threeDecimalValueString(_ total:String) -> String {
let myFloat = (total as NSString).floatValue
return String(format: "%.3f", myFloat)
}
static func fourDecimalValueString(_ total:String) -> String {
let myFloat = (total as NSString).floatValue
return String(format: "%.4f", myFloat)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment