Relative Content

Tag Archive for c++sum

c++ summ up some float values using 2 decimals

I try to sum upp some float values in c++, 65769.59+13.95 = 65783.54 using calculator, but i get 65783.55 in my c++ code why?
I tried
float t_amount = 13.95;your text
float total_amount = 65769.59;
char buf[20];
sprintf_s(buf, “%.2f”, t_amount);
total_amount += (float)atof(buf);

why the result is wrong

for (int col = 0; col < 9; col++) { for (int row = 0; row < 3; row++) { dataGridView5->Rows[3]->Cells[“col8”]->Value=Convert::ToString(Convert::ToDouble(dataGridView5->Rows[2]->Cells[“col8”]->Value)+Convert::ToDouble(dataGridView5->Rows[1]->Cells[“col8”]->Value)+Convert::ToDouble(dataGridView5->Rows[0]->Cells[“col8”]->Value)); } this code for sum cells if all cells has value more than 0 the code is working but if any cell value = 0 the code give the result plus 1 ex… cell1=5 […]