- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift program to Convert Fahrenheit to Celsius
Fahrenheit is the commonly used temperature-measuring unit. In this scale, the freezing point and boiling point of the water are 32 degrees and 212 degrees. Whereas Celsius is also a temperature-measuring scale. In the Celsius scale, the freezing point and the boiling point of the water are 0 degrees and 100 degrees. So in Swift, we can convert Fahrenheit to Celsius using the following methods −
Using Formula
Using converted() method
Method 1: Using Formula
We can easily convert Fahrenheit to Celsius using the mathematical formula. It is the easiest way to convert Fahrenheit to Celsius.
Syntax
T(°C) = (T(°F) - 32) × 5/9
Here, T(°C) represent the temperature in Celsius and T(°F) represents the temperature in Fahrenheit.
Algorithm
Step 1 − Create a function which takes Fahrenheit value as input.
Step 2 − Inside the function, using the conversion formula convert Fahrenheit to Celsius.
Step 3 − Return the final result.
Step 4 − Create a variable to store the value of Fahrenheit temperature.
Step 5 − Call the function and pass the temperature into it.
Step 6 − Display the result.
Example
In the following Swift program, we will convert Fahrenheit to Celsius. So we will create a function which takes Fahrenheit value as input and then convert Fahrenheit into Celsius using ((f-32)*5/9) and return the final result.
import Foundation import Glibc func fahrenheitToCelsius(f: Double) -> Double { return (f - 32) * 5/9 } let temperature: Double = 95.4 let result = fahrenheitToCelsius(f: temperature) print("\(temperature)°F is equal to \(result)°C")
Output
95.4°F is equal to 35.22222222222222°C
Method 2: Using the converted() method
Swift provides good support to convert units without knowing the formula of the conversion with the help of an inbuilt Measurement structure. So to convert Fahrenheit to Celsius we create an instance of Measurement structure and then use the converted() function to convert Fahrenheit to Celsius.
Syntax
func converted(to: .celsius)
Here, we call this function on the Measurement instance and convert Fahrenheit to Celsius.
Algorithm
Step 1 − Create a function which takes Fahrenheit value as input.
Step 2 − Inside the function, using the conversion formula convert Fahrenheit to Celsius.
Step 3 − Return the final result.
Step 4 − Create a variable to store the value of Fahrenheit temperature.
Step 5 − Call the function and pass the temperature into it.
Step 6 − Display the result.
Example
In the following Swift program, we will convert Fahrenheit to Celsius. So we will create an instance of Measurement using the Measurement() constructor and pass the value of Fahrenheit temperature and the unit of measurement. Then we will convert Fahrenheit to Celsius using the converted() function and store the value in the new variable. And display the output.
import Foundation import Glibc let fahrenheitTemp = Measurement(value: 78.0, unit: UnitTemperature.fahrenheit) let celciusTemp = fahrenheitTemp.converted(to: .celsius).value print("So, \(fahrenheitTemp)is equal to \(celciusTemp)Celsius ")
Output
So, 78.0 °F is equal to 25.555555555557987 Celsius
Conclusion
So this is how we can convert Fahrenheit to Celsius. This conversion is very useful in various situations like weather Forecasts, Cooking, science and engineering, etc. Here both methods return the accurate result you can choose any of the methods according to your preference.