Hello CodeCrew Community,
I am having trouble setting a character limit for a dynamic piece of text; not a TextField, just the simple Text(“”) view model in Swift that draws a piece of data from an API. From doing a lot of research, I have found many ways to limit the number of characters within a TextField, but nothing that straightforwardly explains how to accomplish this with a Text(“”) view. It seems like a single modifier added to the Text element would be able to accomplish this, but I can’t seem to find one. I did try using the round() modifier, but if I do that, I lose the decimal placeholder.
My objective, in context, is to get rid of trailing zeros from rating data from the API I am using without losing the first decimal place: for example, 4.5 instead of 4.5000000 and 5.0 instead of 5.0000000. Here is the code:
HStack {
Text("\(business?.rating ?? 0)")
.padding(.leading)
}.padding(.bottom, 16)
Note: The number rating is to the right of the stars rating.
Thanks! Let me know if there’s any more info I should include.
-Michael