WeatherKit Temperature Celsius

Hello everyone,

with Weatherkit I want to display the current Temperature using the system temperature unit setting.

Text(weather.currentWeather.temperature.formatted(.measurement(width: .narrow)))

I have two problems with this code:

  • I ran my code on different simulators: iPhone 14 Pro, iPhone 14 pro max and iPhone 13 Pro Max. On the first simulator it shows a temperature with only number and degree, like 34º, which it supposed to be. But on other two simulators it is shown as 34ºF (system temperature unit set to Fahrenheit). On my actual device it shows again 34º. That makes me very confused.

  • If changed system unit to celsius, the celsius degrees are not rounded, they are like 3,9ºC or 12,3ºC. This is very strange because if its Fahrenheit they are again rounded.

With .measurement(width:.narrow) shouldn't they all work under same condition like rounded or not? or can someone help me how can I have this settings work systematically as it should be?

I made some more test to observe the differences between locations using celsius as temperature unit on iPhone 14 pro simulator:

  • when use a location like lat: 25, lon: -70, which is in the middle of ocean and can not be refer to any city, the weather data from WeatherKit will give me perfectly rounded celsius degrees.

  • When use a location like lat: 40, lon: -90.56, apple suggests this is a place called Rushville and it shows again not rounded celsius degrees.

My guess is if it is near civilization (case Rushville vs Ocean) with much more accurate weather data, WeatherKit tends to give more accurate info, which is intentionally not bad but we don't necessarily need that kind of information. But still I can't find a way around it.

I hope these information could help to address the issue.

new update:

I have managed to narrow down the issue: when using .formatted(.measurement(width: .narrow))) to stylize the temperature which is provided from WeatherKit, if the number is smaller than 10, it will somehow forced to shown with one digit behind decimal, like 9.3 or 7.2. If the number is greater than 10 it will just show you the rounded numbers. It has nothing to do with celsius or Fahrenheit nor with civilization I mentioned above. It is just that Fahrenheit usually have two digits and celsius in my region can easily down to below 10 in winter. so basically with mentioned format style you will see digits behind decimal, when temperature lies between -10 to 10 degrees

Thought that I have found the possible problem, but with my fresh swiftui knowledge i still can't solve it properly. Hope you guys would have more ideas.

solution:

Try this: .measurement( numberFormatStyle: .number.precision(.fractionLength(0)) )

Actually what I am seeing is that in the preview my temps are rounded to whole number in Fahrenheit, but once I push it to my iPhone 13 pro, most temps are showing with 3 decimal places??? I tried the above modifier but that did not work. Maybe I am applying it wrong.

WeatherKit Temperature Celsius
 
 
Q