How can I have a CGFloat only display 2 decimal points in Text

Ok, even though I am EXTREMELY frustrated with this at this point.

I figured I would ask:

How can I get a CGFloat to display (in a text field) with only 2 decimal points.

So:

@State var PercentageValue : CGFloat = 15.338876

Text("\(PercentageValue)"-->displays: 15.338876

I want Text("\(PercentageValue)" to display 15.33


I've tried converting it with the String(NSString) and that gave me errors

I've tried using NumberFormatting and that gave me errors when I was tring to define

numberFormat.maximumFractionDigits = 2 -->Error: Cannot convert value of type '()' to closure result type '_' (what ever that means...)

Any help is appreciated.

Accepted Reply

GOT IT! Finally!

Text("\(PercentageValue, specifier: "%.2f")")

Suggestion is have things like specifier, come up in the drop down/type ahead stuff.

Replies

GOT IT! Finally!

Text("\(PercentageValue, specifier: "%.2f")")

Suggestion is have things like specifier, come up in the drop down/type ahead stuff.