Floating Point Error

Asked

Viewed 1,818 times

4

Follows the code of total price that is giving 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 acquire a certain value to get the troco of the subtraction of two variables:

Below is the code of amount paid:

//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 case it would be the mathematical function of change and some of the lines of StringGrid, is giving floating point error:

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

That I’d be missing?

  • Could you post which error is occurring? A description, and possibly the error line?

  • @carlosrafaelgn, in this case would be a Floating point error, which looks like this.. "'Value' is not a Valid floating point value". This giving this error in the total value.

  • I’m sorry, but I still can’t figure out which line the error might be on. Just from the description, it seems a mistake of "," and ".".... But I’m guessing, here :)

  • No problem @carlosrafaelgn, I found the floating point problem, it was trying to convert a value into string, and it wasn’t working, but the value was in cents, the only value I should convert is the value "CHANGE"! , the rest I should divide and find the correct value, ie Unit= "Split by 1000" and Price= "Split by 100". But thank you for your attention!!

  • Problem-free! :)

  • Just an addendum: You don’t have to FormatFloat('##,###,##0.00',troco), just do FormatFloat(',0.00',troco) that he will put each separate thousand in its proper place

Show 1 more comment

1 answer

2


The function StrtoFloat will accept a string as follows:

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

Already, this way occurs the floating point error:

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

Another form of which it is not accepted is thus:

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 thousand separation point. That way it’s not accepted.

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

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

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

In trying to transform that value string in a double again you already run over the third example I showed on the StrToFloat and then receives the Exception.

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

What wouldn’t 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.