I am trying to code a weather app in swift and I keep getting this
error: Value of type 'Double' has no member 'roundDouble'.
This is the line resulting in the error
WeatherRow(logo: "thermometer", name: "Max temp", value: (weather.main.tempMax.roundDouble() "°"))
struct MainResponse: Decodable {
var temp: Double
var feels_like: Double
var temp_min: Double
var temp_max: Double
var pressure: Double
var humidity: Double
}
CodePudding user response:
The syntax to round is round(var)
, not var.roundDouble()
. (example)
Using the following function call: round(weather.main.tempMax)
CodePudding user response:
Instead of converting a Double
to String
, use a formatter. In this case you can save yourself some work by using MeasurementFormatter
, example:
let formatter = MeasurementFormatter()
formatter.numberFormatter.maximumFractionDigits = 0
let maxTemperature = 42.54
let measurement = Measurement(
value: maxTemperature,
unit: UnitTemperature.celsius
)
let formattedTemperature = formatter.string(from: measurement)
print(formattedTemperature) // 43°C