Search code examples
swiftnsnumberformattereureka-forms

NSNumberFormatter changes 0.5 to .50


My tension is from Eureka (https://github.com/xmartlabs/Eureka) with the usage of a NSFormatter.

My application changes a 0,75 to 0 on the first try and 1,75 to 1.00

After this, I tried multiples things like

let formatter = NumberFormatter()
formatter.groupingSeparator = "."
formatter.minimumFractionDigits = 2
formatter.alwaysShowsDecimalSeparator = true

or with formatter.numberStyle = .decimal

I don't know how I can tell :

"Okay, now 1,75 equals 1.75and 0,75 equals 0.75"

My Decimal row with Eureka :

        timeField = DecimalRow(){ row in
           row.title = "Duration"
           row.placeholder = "1.25"
           row.value = timesheet.time
        }

Thanks in advance !


Solution

  • The probable problem is that you are not setting locale on the formatter and you are not setting decimal separator either. I would assume the decimal separator will be ., too.

    Therefore 0,75 gets parsed as 0 because the , character is unknown.

    The following should fix your problem:

    formatter.decimalSeparator = ","