Last active
April 22, 2022 04:40
-
-
Save jimbrayrcp/f1aae6c996330a4ea4fd28020e87cdd5 to your computer and use it in GitHub Desktop.
Dynamically removes trailing zeros from doubles in swiftui view text field. To use a (func) type extension instead : snippetslab://snippet/5D3360F4-38A6-4504-8B98-86F441E1E931/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct RowView: View { | |
var coin: Coins | |
var body: some View { | |
HStack(alignment: .bottom) { | |
Spacer() | |
Text("\((dynamicDrecimalFormat(dynamicDrecimal: Double(coin.paymentThreshold))))") | |
Text(coin.symbol ?? "") | |
.font(.footnote) | |
} | |
} | |
func dynamicDrecimalFormat(dynamicDrecimal: Double) -> String { | |
Formatter.dynamicDrecimal.string(for: dynamicDrecimal) ?? "" | |
} | |
} | |
extension Formatter { | |
static let dynamicDrecimal: NumberFormatter = { | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .decimal | |
formatter.groupingSeparator = "," | |
formatter.minimumFractionDigits = 0 | |
formatter.maximumFractionDigits = 16 | |
return formatter | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment