How to round a float value in Firemonkey Mobile Delphi XE6?

Asked

Viewed 694 times

2

I am making a mobile application of orders for android in Delphi XE6, in this application I make a discount calculation, which divides the amount of Discount given by 100 less the total value, but the price value is already coming brittle from the database of ex customer : 11,568.

I wanted to know how do I top up this value?!?! ; I found examples using 'round' but it rounds the values more.

  • I agree with Guilherme Bernal, if you need a higher value use int64 for the presentation to the end user divided by 100 and use the function FormatFloat.

2 answers

4


Never never never never use a float to treat monetary values.

They do not keep values accurately that one expects of money, it is well expected that after some calculations sum up a penny or two. The correct is to store the total number of cents as an integer. So R$5.00 turns 500 and R$19,90 turns 1990. Keeping these values always as integers, throughout any calculation you do, you will not need to round or there will be problems of accuracy.

2

The Round accepts two parameters. The second is the number of decimal places you want in the result.

For example, if you do:

Round(11.568, 2);

Will get 11.57 (or 11.57 when on display in Brazilian culture).

The ideal type to work with monetary values in Delphi, Interfacing, is the Currency.

  • yes the round for Delphi admits two parameters, but for firemonkey mobile it accepts only one !!! ; I will try to use Currency, vlw thanks !

  • @Paul See if the Roundto is available. Would look like this: RoundTo(11.568, -2) (minus two in the second parameter).

  • The Roundto is not this, only the round is available for mobile, Thanks again !

Browser other questions tagged

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