 Home
 Products
 Integration
 Tutorial
 Barcode FAQ
 Purchase
 Company
c# net qr code generator Decimal floating point in C#.NET
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.comQRCode Recognizer In C#.NET Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications. www.OnBarcode.comThe 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 floatingpoint types, any number that can be written as a 28digit (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 tradeoff 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 25. 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 PDF417 2d barcode image in .NET applications. www.OnBarcode.comMaking Linear Barcode In C# Using Barcode creator for .NET framework Control to generate, create Linear 1D Barcode image in .NET framework applications. www.OnBarcode.comstatic 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.comGenerating Barcode In C#.NET Using Barcode creation for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications. www.OnBarcode.comNow 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.comUSPS 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.comExpressions 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.comRecognizing Denso QR Bar Code In Visual Basic .NET Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET applications. www.OnBarcode.comAn 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.comMake EAN / UCC  13 In None Using Barcode encoder for Software Control to generate, create UCC  12 image in Software applications. www.OnBarcode.com5.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.comEAN13 Generation In None Using Barcode creator for Software Control to generate, create GTIN  13 image in Software applications. www.OnBarcode.comis 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 26. Generating Barcode In None Using Barcode encoder for Microsoft Excel Control to generate, create Barcode image in Office Excel applications. www.OnBarcode.comEncoding GS1 128 In .NET Framework Using Barcode creator for VS .NET Control to generate, create USS128 image in .NET applications. www.OnBarcode.comkmTravelled / 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.comRecognizing PDF 417 In None Using Barcode reader for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comThe / 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 27 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.comBarcode Drawer In Java Using Barcode maker for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.com60 / 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 FloatingPoint Division
There s a subtle difference between how division works in Examples 26 and 27. Since the two literals in Example 27 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 floatingpoint, it will use a floatingpoint 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 floatingpoint division, which can deal with nonwhole results. If you wanted to use floatingpoint calculations with literals, you could write: 3.0/4.0 The decimal point indicates that we want floatingpoint numbers, and therefore floatingpoint 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.

