0

I would like to change the input format of my textfield.

I want to give my textfield a currency format, like this:

$5 $55 $556 $5.560 $55.560 $556.001

This is my code:

func currencyToString() -> String? {
        let formatter = NumberFormatter()
        formatter.locale = Locale(identifier: "es_AR")
        formatter.numberStyle = .currency
        formatter.usesGroupingSeparator = true
        return formatter.string(from: NSNumber(value: Int(self) ?? 0)
    }

This is my ViewController with RxSwift, But i think this is wrong, because when i run the app i see $0.00 and then when i type 5 for example I get $50.00.

_view.amountTextField.rx.controlEvent(.editingDidBegin)
            .subscribe(onNext: { [weak self] in
                let cur = self?._view.amountTextField.text?.currencyToString()
                self?._view.amountTextField.text = cur

            }).disposed(by: disposeBag)

AmountTextField:

        _view.amountTextField.rx.text.orEmpty
            .withUnretained(self)
            .subscribe(onNext: { owner, amount in
                
                owner.viewModel.configureLabelAlert(amount: amount, label: owner._view.alertLabel, button: owner._view.dateButton)
                
                }).disposed(by: disposeBag)

View Model:

func configureLabelAlert(amount: String, label: UILabel, button: UIButton) {
        minAmount.observe(on: MainScheduler.instance)
            .subscribe(onNext: { minAmount in
        
                let minAmounInt = minAmount / 100
                let amountInt = Int(amount) ?? 0
                
                if amountInt > 0 && amountInt < minAmounInt {
                    label.isHidden = false
                    button.isEnabled = false
                    button.alpha = 0
                } else {
                    label.isHidden = true
                    button.isEnabled = true
                    button.alpha = 1
                }
            })
            .disposed(by: disposeBag)
    }
akitainu22
  • 95
  • 1
  • 8
  • If I dump your extension code into Playgrounds it `"5".currencyToString()` prints `"$ 5,00"`, which suggests that something else is going on – MadProgrammer Jan 21 '22 at 01:53
  • You need to set your formatter [maximumFractionDigits](https://developer.apple.com/documentation/foundation/numberformatter/1415364-maximumfractiondigits) to zero – Leo Dabus Jan 21 '22 at 02:04
  • Btw possible duplicate of [How to input currency format on a text field (from right to left) using Swift](https://stackoverflow.com/questions/29782982/how-to-input-currency-format-on-a-text-field-from-right-to-left-using-swift/29783546#29783546) – Leo Dabus Jan 21 '22 at 02:08
  • It's kind of odd attaching this to `.editingDidBegin`... – Daniel T. Jan 21 '22 at 12:48
  • You are not showing the code that is causing the problem. Are you using `rx.text` somewhere maybe? – Daniel T. Jan 22 '22 at 13:07
  • Yes, Im using rx.text in the view controller, I already edited the post with the code. – akitainu22 Jan 22 '22 at 15:04

0 Answers0