Calculation errors
-
Is something wrong or is this OK. If I add two variables with a data type of single and like (845.12 + 312.14) and sent this directly to a text property of a label the display shows 1157.26. But if I store the sum into a variable with a data type of Double then display, it shows 1157.26000976563. Am I not allowed to convert a single to a double?
-
Is something wrong or is this OK. If I add two variables with a data type of single and like (845.12 + 312.14) and sent this directly to a text property of a label the display shows 1157.26. But if I store the sum into a variable with a data type of Double then display, it shows 1157.26000976563. Am I not allowed to convert a single to a double?
You are allowed to do this, but because of representation differences, you'll get results like these. The best way to do this is to use consistant data types. If the results of your calculations are requiring doubles, then use doubles for all the operands involved, not singles. This is directly from the VS.NET 2003 documentation: A conversion from a value type stores a copy of the source value in the destination of the conversion. However, this copy is not an exact image of the source value. The destination data type stores values differently, and even the value being represented might change, depending on the kind of conversion being performed. RageInTheMachine9532
-
You are allowed to do this, but because of representation differences, you'll get results like these. The best way to do this is to use consistant data types. If the results of your calculations are requiring doubles, then use doubles for all the operands involved, not singles. This is directly from the VS.NET 2003 documentation: A conversion from a value type stores a copy of the source value in the destination of the conversion. However, this copy is not an exact image of the source value. The destination data type stores values differently, and even the value being represented might change, depending on the kind of conversion being performed. RageInTheMachine9532
The problem is that I am pulling data from Microsoft Access. No matter what data type I use in Access I can only place that data in a Single data type within VB. I used a little cheatiing by multiplying the return value by 100 and then transfering to a double and then deviding by 100. This if fine if the data in Access is currency but other data would just have to only be transfered to a VB single data type.