I have some JSON that I would like to reformat before use, preferably in an initializer (or extension ??)
[
{
"name": "Diesel",
"id": "1",
"maj": "2022-07-06 18:28:29",
"value": "2.81"
},
{
"name": "SP95",
"id": "5",
"maj": "2022-07-06 18:28:29",
"value": "2.048"
}
]
I would like to ensure that the "value" data is always 3 decimal places. So in the above 2.810 instead of 2.81.
I have looked at CustomStringConvertible and in theory it looks possible, but I haven't managed to build a working version.
Mainly working from here https://www.swiftbysundell.com/articles/formatting-numbers-in-swift/
my Model looks like this :
struct Price: Codable {
let name: String
let id, maj: String?
var value: String?
var isCheapest: Bool?
}
I understand that I need to do some basic number formatting but I don't see how to integrate it with CustomStringConvertible or if this is the correct way to go about it. Any help appreciated.