Issue
I want to convert Decimal to String in Swift 5.
Since I directly tried
String(aDecimalNumber)
Then Xcode gives error message saying:
Decimal needs to conform LosslessStringConvertible
so I add an extension looks like:
extension Decimal: LosslessStringConvertible {
public init?(_ description: String) {
}
}
But it won't work. My understanding is to extract the description, which is of String type, to a Decimal. But not sure how to do this?
Solution
Here is a possible implementation:
extension Decimal: LosslessStringConvertible {
public init?(_ description: String) {
guard let decimal = Decimal(string: description) else {
return nil
}
self = decimal
}
}
let d: Decimal? = Decimal("1e9") //1000000000
Answered By - ielyamani Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.