-4

I have a string

let str = "2.1"

I want to cast it in Double and always have 2 digits after the separator like that :

let double = 2.10 

So what I do is :

let formatter = NumberFormatter()
formatter.decimalSeparator = "."
formatter.minimumFractionDigits = 2

let myDoubleValue = formatter.number(from: str)?.doubleValue

And the output is still with only 1 fraction digit : 2.1

why ?

Bhaumik
  • 1,218
  • 1
  • 11
  • 20
user3722523
  • 1,740
  • 2
  • 15
  • 27
  • 3
    `2.1` and `2.10` are identical values. minimumFractionDigits is only relevant when converting a number to a *string* with the decimal representation. – Martin R Mar 01 '19 at 21:42
  • 1
    If you care about the rightmost `0` you shouldn't "cast" it to double, you should keep it as a String – Gonzo Mar 01 '19 at 21:56

2 Answers2

1

Perhaps what you mean is:

let str = "2.1"
if let double = Double(str) as? NSNumber {
    let formatter = NumberFormatter()
    formatter.decimalSeparator = "."
    formatter.minimumFractionDigits = 2
    if let myNewStringValue = formatter.string(from: double) {
        print(myNewStringValue) // 2.10
    }
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • ok but I want a double so you are making : string > double > string > double. I would think there is a better way – user3722523 Mar 01 '19 at 22:17
  • @user3722523 If you want a `Double`, you can't have a 0 at the end - it's mathematically meaningless, only relevant as part of the string representation. What exactly are you trying to do where you need to display the 0 but also keep it a `Double` for some reason? – John Montgomery Mar 01 '19 at 22:32
  • the user enter 1.8 in the textField of my tableView (so its a string). Then the value is registered in a realm database as a double. Then the value is displayed on a PDF invoice. And I don't to have a number with 1 fraction digit and another with 2 fraction digits on the same invoice. – user3722523 Mar 02 '19 at 00:43
  • There is no need to create a `NSNumber` object. Just use `Formatter`s method `string(for: Any)` and pass your `Double` to it. `if let double = Double(str) {` `if let myNewStringValue = formatter.string(for: double) {` – Leo Dabus Mar 02 '19 at 00:51
  • @user3722523 But you don't need it to be a `Double` when you display it on the PDF, do you? Convert it and add the 0 when you build the PDF, or when you pass the data to whatever code handles that. – John Montgomery Mar 02 '19 at 01:00
  • yes I think I will add a 0 just before displaying the value. But I wanted to know if there was a solution with NumberFormatter that is design to ... format number... – user3722523 Mar 02 '19 at 01:04
  • 1
    @user3722523 Listen closely. There is no such thing as formatting a number. You cannot "see" a number. There is nothing to format. It is just a quantity. What you format is a _string_ — a visible _representation_ of a number. Two things are two things; two is a _fact_. "2" is a way of _writing_ and _showing_ how many things there are. "2.10" is a _string_, not a number. – matt Mar 02 '19 at 01:34
0

You cannot have a double with trailing 0's, it will truncate.

If you're simply displaying the value, keep it as a string. If you're performing calculations, what difference would the 0's make?

highboi
  • 673
  • 7
  • 28