-1

I am trying to reduce the amount of characters in a double. How Would I reduce this:

59.5220000

to

59.5

this in swift?

2
  • 1
    You should look into the NumberFormatter class. It may look daunting because it uses another class NSNumber, but NSNumbers are easily initialized from Doubles Commented Jun 5, 2019 at 18:34
  • Are you simply trying to round the result to once decimal place in the user interface? Or do you really need the number rounded for other reasons? Commented Jun 5, 2019 at 20:30

2 Answers 2

2

A double doesn't have characters. A string rendering of it does. Rather than using the standard String() initializer (which is really only for development use, it's terrible for end-users), use NumberFormatter.

Sign up to request clarification or add additional context in comments.

Comments

0

perhaps use a formatted string?

let str = String(format: "%.2f", 59.5220000)

1 Comment

If this string is for display in the user interface, this String(format:_:) is not localized. NumberFormatter is much better solution for displaying results in the UI...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.