Hi,
I have 12 textboxes which a user must use to input monthly budget amounts. I then have a button which adds up these amounts, and if they total a pre-set annual budget amount then a save occurs. Most of the time this works fine, but I'm now finding that when the annual budget is £250 and the 12 monthly values are 6 x £35.71, 1 x £35.74 and 5 x £0.00 I get an odd result.
Basically, the following code loops through the 12 textboxes converting the value to a double and adding it to a cumulative total. On the 6th loop (when i = 5 below) it suddenly converts the text "35.71" to the double 35.71000000000002 ? ? ? That's even after converting the same text string in other textboxes correctly. Does anyone have any idea why this is happening? The loop code is very simple and I reproduce it here.
Tom
I have 12 textboxes which a user must use to input monthly budget amounts. I then have a button which adds up these amounts, and if they total a pre-set annual budget amount then a save occurs. Most of the time this works fine, but I'm now finding that when the annual budget is £250 and the 12 monthly values are 6 x £35.71, 1 x £35.74 and 5 x £0.00 I get an odd result.
Basically, the following code loops through the 12 textboxes converting the value to a double and adding it to a cumulative total. On the 6th loop (when i = 5 below) it suddenly converts the text "35.71" to the double 35.71000000000002 ? ? ? That's even after converting the same text string in other textboxes correctly. Does anyone have any idea why this is happening? The loop code is very simple and I reproduce it here.
Code:
for (int i = 0; i < arrMonthBoxes.Length; i++)
{
// if no value has been entered set it to zero
if (arrMonthBoxes[i].Text.Length == 0)
{
arrMonthBoxes[i].Text = "0";
}
// add up all user entered values
userDefinedTotal += Convert.ToDouble(arrMonthBoxes[i].Text);
}
Tom
