Delphi herring values

Asked

Viewed 838 times

1

I have the following problem:

                  | Result |
10     / 0,9280 = | 10,77  |
10     / 0,8740 = | 11,44  |
214,35 / 0,9280 = | 230,98 |

Note: This information is recorded on the rounded server, like the example.

Now if I take this information and flip to show the user the original value:

                   |  Result   |Correto |
10,77   * 0,9280 = | 9,99456   |  10    |
11,44   * 0,8740 = | 9,99856   |  10    |
230,98  * 0,9280 = | 214,34944 | 214,35 |

How I can make this round?

  • you could round with the Roundto method in this way, following example: Roundto (1.234, -2); //returns 1.23, so if you round Roundto(9.99456, -3) I think you get the result.

  • Managed to solve friend?

2 answers

1

I believe I can use the function System.Math.Setroundmode. According to the documentation, he can assume rmNearest, rmDown, rmUp or rmTruncate. The documentation gives an example here.

SetRoundMode(rmNearest); // Arredonda para o valor mais próximo

0

You are saving the result with only two decimal places, evidently the subsequent multiplication will take into account only the two houses and consequently will not reach the desired value.
As an example, see:

Correct:

10     / 0,9280 = | 10,77586206896552

Your code

10     / 0,9280 = | 10,77
  • Yes, but I’m not the one who records that information. I’m looking for a way to take this server value with only two decimal places and convert it to the original value.

  • There is no way with 100% guarantee... you can try to round the end value, ex.: StrToFloat(FormatFloat('0.00',9.99456)) = 10. But it is not guaranteed to arrive at the amount you want...

  • If the third number after the comma was less than 5, change to 5 in case it would be: Strtofloat(Formatfloat('0.00',9.99556)) = 10. Wouldn’t it work that way?

  • In this specific case yes, but the point is that you should not always round up. In your case I see no way out.

Browser other questions tagged

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