Floating Point Error

4

Here is the code for the total price that is giving the floating-point error ':

//Pr. Total
sgItens.Cells[5,l] := FormatFloat('###,###,###,##0.00',StrToFloat(copy(lTemp, 210, 14)));
cont:=  StrToFloat(sgItens.Cells[5,l]);
valorTotal:= valorTotal+cont;

I'm trying to get a certain value to get troco of subtraction of two variables:

Here is the code for the Paid value :

//Valor Pago
sgFinalizadoras.Cells[3,l] := FormatFloat('##,###,##0.00',StrToFloat(copy(lTemp, 80, 13)));
valorPago:=  StrToFloat(copy(lTemp, 80, 13));

And just below the code to perform the subtraction, in the case would be the mathematical function of change and some of the lines of StringGrid , this is giving floating point error:

//Troco
troco:= valorPago - valorTotal;
Edit2.Text := FormatFloat('##,###,##0.00',troco);

What would I be wrong about?

    
asked by anonymous 08.05.2014 / 19:51

1 answer

2

The StrtoFloat function will accept a string of the following form:

StrToFloat('9999,99');  // --> Assim é aceito

Already, this way the floating point error occurs:

StrToFloat('9999.99');  // --> Assim NÃO é aceito

Another form of which is not accepted is:

StrToFloat('9.999,99');  // --> Assim NÃO é aceito

In the latter, although the decimal separator character is the comma as in the first example in which it is accepted, there is also the separation point of thousands. That way it is not accepted.

Then in your example you get a value in string and convert it to double , already formatting to become presentable with FormatFloat :

sgItens.Cells[5,l] := FormatFloat('###,###,###,##0.00',StrToFloat(copy(lTemp, 210, 14)));

What, depending on the value, can become something like: 99.999,99 .

When trying to transform this value string into a double again you already run over the third example I showed on StrToFloat and then get Exception .

cont := StrToFloat(sgItens.Cells[5,l]);

What would not happen if you did something like:

//Pr. Total
cont := StrToFloat(copy(lTemp, 210, 14));
valorTotal := valorTotal+cont;

sgItens.Cells[5,l] := FormatFloat('###,###,###,##0.00', cont);
    
09.05.2014 / 13:39