setTitle for Buttons not uses complete number.

I want to set the title for a button with a double number.

The problem is that the function always cuts a zero, if the last number is a 0.


var number1: Double = 3.99
var number2: Double = 4.30

button.setTitle(\(number1), for: .normal) //Title is 3.99
button.setTitle(\(number2), for: .normal) // Title is 4.3  but hast to be 4.30


I tried to convert it with String(number2), but the title is 4.3 too.

Accepted Reply

Just replace by this


     button1.setTitle(String(format: "%.2f", number1), for: .normal) // Title is 3.99
     button2.setTitle(String(format: "%.2f", number2), for: .normal) // Title is 4.30


Did your code compile as it was written ?

Didn't you get an error as:

Cannot convert value of type 'WritableKeyPath<_, _>' to expected argument type 'String?'


Note: which version of XCode do you use ?

Replies

Just replace by this


     button1.setTitle(String(format: "%.2f", number1), for: .normal) // Title is 3.99
     button2.setTitle(String(format: "%.2f", number2), for: .normal) // Title is 4.30


Did your code compile as it was written ?

Didn't you get an error as:

Cannot convert value of type 'WritableKeyPath<_, _>' to expected argument type 'String?'


Note: which version of XCode do you use ?

I don´t get an error, it´s working fine.

I´m using xcode 9.4.1.