# c# net qr code generator Decimal floating point in C#.NET Print QR Code 2d barcode in C#.NET Decimal floating point

Decimal floating point
QR Code ISO/IEC18004 Generator In C#
Using Barcode generator for Visual Studio .NET Control to generate, create QR Code 2d barcode image in .NET applications.
www.OnBarcode.com
QR-Code Recognizer In C#.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
The decimal type (or System.Decimal, as .NET calls it) is superficially very similar to double and float, except its internal representation is adapted to decimal representations. It can represent up to 28 decimal digits of precision, and unlike the two binary floating-point types, any number that can be written as a 28-digit (or fewer) decimal can be represented completely accurately as a decimal variable. The value 0.1 fits comfortably into 28 digits with room to spare, so this would fix the problem in the previous example. The decimal type still has limited precision; it just has less surprising behavior if you re looking at all your numbers in decimal. So if you are performing calculations involving money, decimal is likely to be a better choice than double or float. The trade-off is that it s slightly less efficient computers are more at home in binary than decimal. For our race information application, we don t have any particular need for decimal fidelity, which is why we re using the double type in Example 2-5. Getting back to that example, recall that we defined three variables that hold the distance our car has traveled, how long it took, and how much fuel it burned in the process. Here it is again so that you don t have to flip back to it:
Print PDF 417 In C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in .NET applications.
www.OnBarcode.com
Making Linear Barcode In C#
Using Barcode creator for .NET framework Control to generate, create Linear 1D Barcode image in .NET framework applications.
www.OnBarcode.com
static void Main(string[] args) { double kmTravelled = 5.141; double elapsedSeconds = 78.738; double fuelKilosConsumed = 2.7; }
Code128 Generator In C#
Using Barcode generator for .NET framework Control to generate, create Code 128B image in VS .NET applications.
www.OnBarcode.com
Generating Barcode In C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Now that we ve looked at the numeric types, the structure of these lines is pretty clear. We start with the type of data we d like to work with, followed by the name we d like to use, and then we use the = symbol to assign a value to the variable. But assigning constant values isn t very exciting. You can get the computer to do more useful work, because you can assign an expression into a variable.
Code39 Maker In C#.NET
Using Barcode drawer for .NET framework Control to generate, create Code 39 image in Visual Studio .NET applications.
www.OnBarcode.com
USPS Confirm Service Barcode Encoder In C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create USPS PLANET Barcode image in VS .NET applications.
www.OnBarcode.com
Expressions and Statements
QR Code ISO/IEC18004 Printer In Java
Using Barcode creation for Java Control to generate, create QR Code 2d barcode image in Java applications.
www.OnBarcode.com
Recognizing Denso QR Bar Code In Visual Basic .NET
www.OnBarcode.com
An expression is a piece of code that produces a value of some kind. We ve actually seen several examples already, the most basic being the numbers we re assigning into the variables. So in our example, a number such as:
QR Code ISO/IEC18004 Creation In Java
Using Barcode generator for Android Control to generate, create QR Code JIS X 0510 image in Android applications.
www.OnBarcode.com
Make EAN / UCC - 13 In None
Using Barcode encoder for Software Control to generate, create UCC - 12 image in Software applications.
www.OnBarcode.com
5.141
Creating Code 128 Code Set B In None
Using Barcode encoder for Microsoft Excel Control to generate, create Code 128 Code Set C image in Excel applications.
www.OnBarcode.com
EAN-13 Generation In None
Using Barcode creator for Software Control to generate, create GTIN - 13 image in Software applications.
www.OnBarcode.com
is an expression. Expressions where we just tell C# what value we want are called literal expressions. More interestingly, expressions can perform calculations. For example, we could calculate the distance traveled per kilogram of fuel consumed with the expression in Example 2-6.
Generating Barcode In None
Using Barcode encoder for Microsoft Excel Control to generate, create Barcode image in Office Excel applications.
www.OnBarcode.com
Encoding GS1 128 In .NET Framework
Using Barcode creator for VS .NET Control to generate, create USS-128 image in .NET applications.
www.OnBarcode.com
kmTravelled / fuelKilosConsumed
Painting 1D In Visual Basic .NET
Using Barcode maker for .NET framework Control to generate, create 1D Barcode image in .NET applications.
www.OnBarcode.com
Recognizing PDF 417 In None
www.OnBarcode.com
The / symbol denotes division. Multiplication, addition, and subtraction are done with *, +, and -, respectively. You can combine expressions together too. The / operator requires two inputs the dividend and the divisor and each input is itself an expression. We were able to use variable names such as kmTravelled because a variable name is valid as an expression the resultant value is just whatever that variable s value is. But we could use literals, as Example 2-7 shows. (A trap awaits the unwary here; see the sidebar on the next page.)
Denso QR Bar Code Creator In .NET
Using Barcode generator for Reporting Service Control to generate, create QR Code image in Reporting Service applications.
www.OnBarcode.com
Barcode Drawer In Java
Using Barcode maker for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
60 / 10
Or we could use a mixture of literals and variable names to calculate the elapsed time in minutes:
elapsedSeconds / 60
or a multiplication expression as one of the inputs to a division expression to calculate the elapsed time in hours:
elapsedSeconds / (60 * 60)
Integer Versus Floating-Point Division
There s a subtle difference between how division works in Examples 2-6 and 2-7. Since the two literals in Example 2-7 do not contain decimal points, the compiler treats them as integers, and so it will perform an integer division. But since the kmTravelled and fuelKilosConsumed variables are both floating-point, it will use a floating-point division operation. In this particular case it doesn t matter, because dividing 60 by 10 produces another integer, 6. But what if the result had not been a whole number If we had written this, for example:
3/4
the result would be 0, as this is an integer division 4 does not go into 3. However, given the following:
double x = 3; double y = 4;
the value of x/y would be 0.75, because C# would use floating-point division, which can deal with nonwhole results. If you wanted to use floating-point calculations with literals, you could write:
3.0/4.0
The decimal point indicates that we want floating-point numbers, and therefore floating-point division, so the result is 0.75.
(The parentheses ensure that we divide by 60 * 60. Without the parentheses, this expression would divide by 60, and then multiply by 60, which would be less useful. See the sidebar on the next page.) And then we could use this to work out the speed in kilometers per hour:
kmTravelled / (elapsedSeconds / (60 * 60))
Expressions don t actually do anything on their own. We have described a calculation, but the C# compiler needs to know what we want to do with the result. We can do various things with an expression. We could use it to initialize another variable:
double kmPerHour = kmTravelled / (elapsedSeconds / (60 * 60));
or we could display the value of the expression in the console window:
Console.WriteLine(kmTravelled / (elapsedSeconds / (60 * 60)));
Both of these are examples of statements. Whereas an expression describes a calculation, a statement describes an action. In the last two examples, we used the same expression a calculation of the race car s speed but the two statements did different things: one evaluated the expression and assigned it into a new variable, while the other evaluated the expression and then passed it to the Console class s WriteLine method.