Rounding out with Formatfloat

Asked

Viewed 594 times

2

When trying to round to 4 decimal places the value 18,36735 by means of the function Formatfloat, the system generates the value 18.3673. How is this possible? It should not round up the value, generating 18.3674? At least in numerous other points of the system the function Formatfloat works as expected, but at this point is occurring this...

inserir a descrição da imagem aqui

Anyway what I want to know is, This is some bug? Is it an expected behavior? And, how to resolve this situation?

Important details (interesting): If the rounded value is changed to 18,38735 the function Formatfloat round off to 18,387 as expected4.
I use Delphi XE7.

1 answer

-1

It’s a normal behavior.

Rounding follows a metric up to 4 round down. From the 5 Airbending.

It’s a pattern, it’s not just in Delphi.

  • Exactly, it was this behavior that I expected, but as you can see in the image and explanation that I provided, it happened exactly the opposite. See in the image that the last decimal is 5, but has not rounded the penultimate decimal place to 4, previously kept the 3. Like?

  • Right! I ran a test " edit1.Text := formatFloat('0.0000',18.36735);" Worked normal. That is, the 3 turned 4. You are probably confusing comma(,) with dot(.).

  • I still don’t understand, because this situation doesn’t make any sense. However, I noticed that the problem occurs when I try to round up a value that comes from a dataset, example: Formatfloat('0.0000',Cdsitens.Fieldbyname('VALUE'). Asfloat);

  • ...has to see the conversion of values, the databases are standardized for the American parameters. Which normally replaces (,) by (.), also the date. Ours is dd.mm.yyyy, where the standard (American) is mm.dd.yyyy. In these cases we use Stringreplace ex: string := Stringreplace(value. Text, '. ', ', ', [rfReplaceAll]);

  • I always thought that the rounding used was the banking (from 1.5 or 2.5 goes to 2, other values no limits follows this other scheme)

Browser other questions tagged

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