Google


   


You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Numbers And Enumerations

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

C# Cookbook, 2nd Edition



Problem: You need to compare a fraction with a value of type double or float to determine whether they are within a close approximation to each other. Take, for example, the result of comparing the expression 1/6 and the value 0.16666667. These seem...



Problem: When using the trigonometric functions of the Math class, all units are in radians. You have one or more angles measured in degrees and want to convert these to radians in order to use them with the members of the Math class. Solution:...



Problem: When using the trigonometric functions of the Math class, all units are in radians; instead, you require a result in degrees. Solution: To convert a value in radians to degrees, multiply it by 180/p: using System; public stati...



Problem: The bitwise complement operator (~) is overloaded to work directly with int, uint, long, ulong, and enumeration data types consisting of the underlying types int, uint, long, and ulong. However, you need to perform a bitwise complement oper...



Problem: You need a simple method to test a numeric value to determine whether it is even or odd. Solution: The solution is actually implemented as two methods. To test for an even integer value, use the following method: public static boo...



Problem: You have a 32-bit integer value that contains information in both its lower and upper 16 bits. You need methods to get the high word (first 16 bits) and/or the low word (last 16 bits) of this value. Solution: To get the high word of a...



Problem: You have a string containing a number in base2 (binary), base8 (octal), base10 (decimal), or base16 (hexadecimal). You need to convert this string to its equivalent integer value and display it in base10. Solution: To convert a number...



Problem: You have a string that possibly contains a numeric value. You need to know whether this string contains a valid number. Solution: Use the static TryParse method of any of the numeric types. For example, to determine whether a string c...



Problem: You need to round a number to a whole number or to a specific number of decimal places. Solution: To round any number to its nearest whole number, use the overloaded static Math.Round method, which takes only a single argument: in...



Problem: The Math.Round method will round the value 1.5 to 2; however, the value 2.5 will also be rounded to 2 using this method. You may always want to round to the greater number in this type of situation (e.g., round 2.5 to 3 instead of 2). Conve...



Problem: You have a temperature reading measured in Celsius and need to convert it to Fahrenheit. Solution: public static double CtoF(double celsius) { return (((0.9/0.5) * celsius) + 32); } To generate a double result while m...



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 ...



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 an int results in a loss of information only if the long data type is g...



Problem: You need to calculate the length of one side of a triangle when either the lengths of two sides are known or one angle and the length of a side are known. Solution: Use the Math.Sin, Math.Cos, and Math.Tan methods of the Math class to...



Problem: You need to calculate an angle of a triangle when the lengths of two sides are known. Solution: Use the Math.Atan, Math.Acos, or Math.Asin static methods of the Math class. The following code calculates the angle theta and returns the...



Problem: You need to display the textual or numeric value of an enumeration member. Solution: To display an enumeration value as a string, use the ToString method that each enumeration member inherits from System.Enum. Using the following Val...



Problem: You have the textual value of an enumeration element, possibly from a database or text file. This textual value needs to be converted to a usable enumeration type. Solution: The static Parse method on the Enum class allows the textual...



Problem: When you pass a numeric value to a method that accepts an enumeration type, it is possible to pass a value that does not exist in the enumeration. You want to perform a test before using this numeric value to determine if it is indeed one o...



Problem: You need to determine if a given value is a valid enumeration value or a valid combination of enumeration values (i.e., bit flags ORed together in an enumeration marked with the Flags attribute). Solution: To make it possible to test ...



Problem: An enumeration of values is needed to act as bit flags that can be ORed together to create a combination of values (flags) in the enumeration. Solution: Mark the enumeration with the Flags attribute: [Flags] enum Language { ...



Problem: You need to determine if a variable of an enumeration type, consisting of bit flags, contains one or more specific flags. For example, given the following enumeration Language: [Flags] enum Language { CSharp = 0x0001, VBNET = ...



Problem: You need to find the integer portion of a decimal or double number. Solution: You can find the integer portion of a decimal or double by truncating it to the whole number closest to zero. To do so, use the overloaded static System.Mat...


SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Numbers And Enumerations


ADBRITE ads links
   
Related tags







Popular Categories
Unix books and guides

AJAX popular information
C# language guides
Windows books and cookbooks

.......








Business Key Top Sites

be number one
rate your site




    С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.

    Русский Polski Francais Deutsch
    support sitemap terms

© CodeIdol Labs, 2007 - 2009