Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CurrencyTextField not showing decimals onChange #9

Closed
LualdiD opened this issue Sep 6, 2020 · 4 comments
Closed

CurrencyTextField not showing decimals onChange #9

LualdiD opened this issue Sep 6, 2020 · 4 comments

Comments

@LualdiD
Copy link

LualdiD commented Sep 6, 2020

I'm trying to update CurrencyTextField based on another textfield but it doesn't seems to work well.
The problem I'm facing is when I updated hourlySalary to a decimal like 0.02 it always round to 0

Also the button in this view show the decimals in hourlySalary just on the initial state, if I start inputting something in any textFields then it just jumps from 2.0 to 0.0 without showing decimals

`
import SwiftUI

struct ContentView: View {

@State var hourlySalary: Double? = 1.0
@State var montlhySalary: Double? = 0.0
@State var yearlySalary: Double? = 0.0

@State var test: Bool = false



var body: some View {
    ScrollView {
        VStack(alignment: .leading, spacing: 20) {
            Text("Hourly")
            CurrencyTextField(currencyString(amount: 0), value: $hourlySalary)
                .font(.title)
            
            Text("Monthly")
            CurrencyTextField(currencyString(amount: 0), value: $montlhySalary)
                .font(.title)
            
            Text("Yearly")
            CurrencyTextField(currencyString(amount: 0), value: $yearlySalary, tag: 10)
                .font(.title)
            
            Button(action: {
                if test {
                    hourlySalary = 0.5
                } else {
                    hourlySalary = 2
                }
                test.toggle()
            }, label: {
                Text("Button")
            })
            
        }
        .onChange(of: hourlySalary ?? 0, perform: { value in
            let hourly = value
            let monthly = (hourly * 8) * 20
            let yearly = monthly * 12
            montlhySalary = monthly
            yearlySalary = yearly
        })
        .onChange(of: montlhySalary ?? 0, perform: { value in
            let monthly = value
            let hourly = (monthly / 20) / 8
            let yearly = monthly * 12
            hourlySalary = hourly
            yearlySalary = yearly
        })
        .onChange(of: yearlySalary ?? 0, perform: { value in
            let yearly = value
            let monthly = yearly / 12
            let hourly = (monthly / 20) / 8
            hourlySalary = hourly
            montlhySalary = monthly
        })
        .padding()
        .navigationTitle("What's your salary?")
    }
}

`

@youjinp
Copy link
Owner

youjinp commented Sep 6, 2020

I've just tested with Xcode 12 beta 6 and iOS 14 beta 6.

Screenshot

Am I seeing something different from what you saw?

@LualdiD
Copy link
Author

LualdiD commented Sep 6, 2020

This is what I'm seeing:

Screen-Recording-2020-09-06-at-2

At the end of the gif I make it work if hourly already have a decimal.
I'm not sure why the button works like that but anyway it's there just as a test.
My main problem is that when I update the yearly field, the hourlySalary fields always gets round up to 0.
For example in the GIF when I type 111 hourly salary become 0.0578125 but I still get 0 in the hourly field, and at 1,111 it should be 0.57 but it rounds it up to 1

@youjinp youjinp closed this as completed in 572d0f4 Sep 6, 2020
youjinp added a commit that referenced this issue Sep 6, 2020
@youjinp
Copy link
Owner

youjinp commented Sep 6, 2020

I fixed it in the latest release, thanks for letting me know about this issue!

@youjinp
Copy link
Owner

youjinp commented Sep 6, 2020

Heres the gif of it working properly:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants