Lazarus
Free Pascal => Beginners => Topic started by: JLWest on July 04, 2020, 07:29:01 pm

Var MPMin, MPHS, Wrk : Single;
begin
MPMin := (MPHs / 60); { = 8.71666622}
WrK := MPMin;
MPMin := Roundto(Wrk); {= 0}
Tried this:
MPMin := RoundTo( 8.71666622 , 2); {= 0}

The function RoundTo is a bit counterintuitive because everybody calling RoundTo(x, 2) thinks that it rounds x to 2 decimal places. No, it rounds to multiples of 10 to the power of the second argument. In other words: 10^2 is 100, and thus RoundTo(x, 2) rounds to full hundreds, e.g. RoundTo(871.6666, 2) becomes 900, but RoundTo(8.71666, 2) becomes 0 because it is less than 100.
In order to round to two decimal places you round to multiples of 0.01 = 10^2. therefore, you must call RoundTo(x, 2).

Hi!
Yes  that is correct.
roundTo (8.7,2) means: round to 100  so it is zero
roundTo (8.7,1) means: round to 10  so it is 10
roundTo (8.7,0) means: round to 1  so it is 9
Everything as it should be.
Winni

A "bit counterintuitive"! I felt bad about posting the question. Now I feel better.
Thank you gentlemen.