Merge pull request #2292 from vladyslav-iosdev/#2291

Remove trailing zeros from Amount Text Field added during switch from crypty to native currency #2291
pull/2250/head
Hwee-Boon Yar 4 years ago committed by GitHub
commit bafef32384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      AlphaWallet/UI/Views/AmountTextField.swift

@ -188,10 +188,10 @@ class AmountTextField: UIControl {
get {
switch currentPair.left {
case .cryptoCurrency:
return textField.text ?? "0"
return textField.text?.droppedTrailingZeros ?? "0"
case .usd:
guard let value = ethCostRawValue else { return "0" }
return StringFormatter().alternateAmount(value: value)
return StringFormatter().alternateAmount(value: value).droppedTrailingZeros
}
}
set {
@ -229,9 +229,9 @@ class AmountTextField: UIControl {
switch currentPair.left {
case .cryptoCurrency:
return StringFormatter().currency(with: amount, and: Constants.Currency.usd)
return StringFormatter().currency(with: amount, and: Constants.Currency.usd).droppedTrailingZeros
case .usd:
return StringFormatter().alternateAmount(value: amount)
return StringFormatter().alternateAmount(value: amount).droppedTrailingZeros
}
}
@ -500,6 +500,22 @@ extension String {
var optionalDecimalValue: NSDecimalNumber? {
return EtherNumberFormatter.full.decimal(from: self)
}
var droppedTrailingZeros: String {
var string = self
let decimalSeparator = Locale.current.decimalSeparator ?? "."
while string.last == "0" || string.last?.toString == decimalSeparator {
if string.last?.toString == decimalSeparator {
string = String(string.dropLast())
break
}
string = String(string.dropLast())
}
return string
}
}
extension EtherNumberFormatter {

Loading…
Cancel
Save