Learn Courses My Dashboard

How can I round this data and display it in text element?

I need to divide two double values and then round the results to the tenths place. How would I go about doing this? Thank you in advance!

Use this version of the String initializer:

String(format: "%.1f", (total / rate))

There are other ways to do it, but this is probably the easiest.

BTW, you don’t need to do this:

private var rate = Double(8.25)
@State private var total = Double(179.14)

8.25 and 179.14 are already implicitly Doubles so you don’t need to convert them.

You can just do:

private var rate = 8.25
@State private var total = 179.14

Or, if you want to make the type annotation explicit:

private var rate: Double = 8.25
@State private var total: Double = 179.14
1 Like

Perfect, Very helpful! thank you!