
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
14
|
Chapter 1: Numbers and Enumerations
Solution
public static double CtoF(double celsius)
{
return (((0.9/0.5) * celsius) + 32);
}
To generate a double result while maintaining the same ratio (9 to 5) as the integers
give, 0.9 and 0.5 are used in the calculation.
Discussion
This recipe makes use of the following Celsius-to-Fahrenheit temperature conver-
sion equation:
TempFahrenheit = ((9 / 5) * TempCelsius) + 32
The Fahrenheit temperature scale is widely used in the United States. However,
much of the rest of the world uses the Celsius temperature scale.
1.12 Converting Fahrenheit to Celsius
Problem
You have a temperature reading measured in Fahrenheit and need to convert it to
Celsius.
Solution
public static double FtoC(double fahrenheit)
{
return ((0.5/0.9) * (fahrenheit - 32));
}
Discussion
This recipe makes use of the following Fahrenheit-to-Celsius temperature conver-
sion equation:
TempCelsius = (5 / 9) * (TempFahrenheit - 32)
The Fahrenheit temperature scale is widely used in the United States. However,
much of the rest of the world uses the Celsius temperature scale.
1.13 Safely Performing a Narrowing Numeric Cast
Problem
You need to cast a value from a larger value to a smaller one, while gracefully handling
conditions that result in a loss of information. For example, casting a
long to