// from a Decimal
let d: Decimal = 4444444.5555555
let s = d.description
// now how to take `s` to create the same Decimal back?
let d2 = Decimal(....) // which init to use?
// I don't think
// init?(string: String, locale: Locale? = nil)
// is correct as it takes a `Locale`
// because Decimal.description is locale independent: decimal point is alway "."
The reason I ask this is because I want to store Decimal with @AppStorage by conforming it to RawRepresentable, RawValue == String. And would like the persistent string to be locale independent going out and coming back. So even if the user change its locale, thing still work.
I believe Decimal.description is locale independent: for a value like 4444444.5555555, it always produce 4444444.5555555, never 4444444,5555555 even if the user's locale use , as decimal point.
But Decimal.init(string:locale) use the locale to parse the string, so it could use a , as decimal point this will incorrectly parse? I don't want that.
One way to do this:
let d2 = Decimal(Double(s)!)
will this lose precision?
Ok, just force a locale that match what .description use:
let d2 = Decimal(string: s, locale: Locale(identifier: "en_US")) // I think "en_US" does the job?