-3

Why does a simple thing like Rounding is so complicated in Swift?

This is an extension on Double.

enter image description here

I tried

  round(self - 273.15) 

and got the same error.

Also tried:

  (self - 273.14).round()
john doe
  • 9,220
  • 23
  • 91
  • 167

2 Answers2

3

The round that you are trying to use is a mutating member of the Double type. Try this instead:

func toCelsius() -> Double {
    return (self - 273.15).rounded()
}

To clarify, a mutating member means that it changes the value itself (and in this case, returns Void). This means that it cannot be called on constants (or the results of computations, as in your case).

The function I am using, rounded(), is not a mutating member, and returns a Double — exactly what you want.

Sam
  • 2,350
  • 1
  • 11
  • 22
  • Yes! Thanks. When I use round it said in the docs that it will return a Double. – john doe Apr 10 '20 at 18:32
  • No problem! Just out of curiosity, do you still have the URL for the docs where you found that (no worries if you don't). If the docs are wrong, I'd like to report it to the maintainers so that nobody else is lead astray – Sam Apr 10 '20 at 18:47
1

The answer is actually offtopic, nevertheless there is a simpler solution for this particular case.

According to your previous question it seems that you are dealing with the openweathermap API.

If you add

units=metric

to the URL query you get all degrees in Celsius.

vadian
  • 274,689
  • 30
  • 353
  • 361