turning off RoundTo uses the Banker's rounding lazarus / 62-General ---------------------------------------------------------------------- tonathiu · 2021-12-15 14:43 It seems that when using the function RoundTo (math unit) it uses the Banker's rounding, for example my column.point = 76.5 returns 76 instead of 77. I use Lazarus 1.8.2 FPC 3.0.4 Ex: result:= Double(column.point * scale)//76.5; SetRoundMode(rmNearest); result:= RoundTo(result,-decimalen)//76; how can I turn off Banker's rounding so the rounding for 76.5 ~= 77 or find a solution to my problem. thanks ---------------------------------------------------------------------- Handoko · 2021-12-15 15:34 I haven't tested it properly but I believe these should work: Rounding to nearest half up: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);const  DecimalLen = 0;  Test     = 76.5;var  R: Double;begin  R := RoundTo(Test + (DecimalLen+1)*0.5, -DecimalLen);  ShowMessage(R.ToString);end; Optional, rounding to nearest half away from zero: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);const  DecimalLen = 0;  Test     = 76.5;var  R: Double;begin  case Test > 0 of    True:  R := RoundTo(Test + (DecimalLen+1)*0.5, -DecimalLen);    False: R := RoundTo(Test - (DecimalLen+1)*0.5, -DecimalLen);  end;  ShowMessage(R.ToString);end; Learn more: https://en.wikipedia.org/wiki/Rounding#Comparison_of_approaches_for_ro unding_to_an_integer ---------------------------------------------------------------------- winni · 2021-12-15 15:46 Hi! RoundTo does something totaly different than round. Already discussed here: https://forum.lazarus.freepascal.org/index.php/topic,50438.msg368109.h tml#msg368109 Winni ---------------------------------------------------------------------- Ally · 2021-12-15 16:03 Hello tonathiu, try the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyRound(X: Double): Int64;begin  Result := Trunc((X * 2 + 1) / 2);end; Greetings Ally ---------------------------------------------------------------------- Warfley · 2021-12-15 16:50 If you always want to round away from 0 on 0.5 you can use the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Trunc(d + 0.5*Sign(d)) But you should know that, even though it is different from the way you have learned rounding in school, bankers rounding reduces the expected error and is therefore usually preferential to "normal" rounding. ---------------------------------------------------------------------- BobDog · 2021-12-19 00:46 You can do it via your c runtime in windows; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i usesmath;const       default=0;    rounddown=256;    roundup=512;    truncicate=768;        function SetRoundMode(new:integer;mask:integer=768):integer cdecl external 'msvcrt.dll' name '_controlfp';     vars1,s2:double;a:integer; beginSetRoundMode(roundup);  for a := 139 to 160 dobegin       s1 := (a / 2);       s2 := round(s1);       writeln(s1 ,'              ', s2);   end;writeln('Press return to end . . .'); readln;end. ---------------------------------------------------------------------- Warfley · 2021-12-19 02:47 Quote from: BobDog on December 19, 2021, 01:46:19 amYou can do it via your c runtime SetRoundMode is also defined in the math unit, so you can also use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SetRoundMode(rmUp);The problem is that this always rounds up, not just on .5 but also on smaller values like .1: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  SysUtils, Math; var  i: Integer;  d: Double;begin  SetRoundMode(rmUp);  for i:=0  to 20 do  begin    d := i / 10;    WriteLn(FloatToStr(d), ' Roudned: ', Round(d));  end;  ReadLn;end. Result: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 0 Roudned: 00,1 Roudned: 10,2 Roudned: 10,3 Roudned: 10,4 Roudned: 10,5 Roudned: 10,6 Roudned: 10,7 Roudned: 10,8 Roudned: 10,9 Roudned: 11 Roudned: 11,1 Roudned: 21,2 Roudned: 21,3 Roudned: 21,4 Roudned: 21,5 Roudned: 21,6 Roudned: 21,7 Roudned: 21,8 Roudned: 21,9 Roudned: 22 Roudned: 2This is basically equivalent to the ceil function (just with the additional caveat that changing the rounding mode of the FPU can also change other internal handling of floating points and should be ommited). The problem is that  the normal rounding rounds to the nearest even integer, so 0.5 is rounded to 0, 1.5 is rounded to 2 like 2.5, 3.5 and 4.5 are rounded to 4 and so on, which is contrary to how roundings is for example tought at most schools where you always round up on 0.5. As far as I know this is the default behavior and there is no way to have a another rounding behavior that rounds numbers < 0.5 down,  numbers >0.5 up and 0.5 in a defined direction ---------------------------------------------------------------------- Josh · 2021-12-19 06:36 hi does SimpleRoundTo in math unit help. delphi doc https://docwiki.embarcadero.com/RADStudio/Sydney/en/Floating- Point_Rounding_Routines ---------------------------------------------------------------------- big_M · 2021-12-21 13:46 I have written these functions some time ago because I needed a different rounding method. So if you need to round to an integer value and not to a specific decimal place these could be useful for you: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit URound2; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Math; function RoundHalfUp(const ANumber: Float): integer;function RoundHalfDown(const ANumber: Float): integer;function RoundHalfToZero(const ANumber: Float): integer;function RoundHalfToInf(const ANumber: Float): integer;  implementation  {Fill a Memo to test:    Memo1.Clear;     Memo1.Append('Round(0.5) = '+inttostr(Round(0.5)));    Memo1.Append('Round(1.5) = '+inttostr(Round(1.5)));    Memo1.Append('Round(-0.5) = '+inttostr(Round(-0.5)));    Memo1.Append('Round(-1.5) = '+inttostr(Round(-1.5)));     Memo1.Append('  ');     Memo1.Append('RoundHalfUp(0.5) = '+inttostr(RoundHalfUp(0.5)));    Memo1.Append('RoundHalfUp(1.5) = '+inttostr(RoundHalfUp(1.5)));    Memo1.Append('RoundHalfUp(-0.5) = '+inttostr(RoundHalfUp(-0.5)));    Memo1.Append('RoundHalfUp(-1.5) = '+inttostr(RoundHalfUp(-1.5)));     Memo1.Append('  ');     Memo1.Append('RoundHalfUp(0.4) = '+inttostr(RoundHalfUp(0.4)));    Memo1.Append('RoundHalfUp(0.6) = '+inttostr(RoundHalfUp(0.6)));    Memo1.Append('RoundHalfUp(-0.4) = '+inttostr(RoundHalfUp(-0.4)));    Memo1.Append('RoundHalfUp(-0.6) = '+inttostr(RoundHalfUp(-0.6)));      Memo1.Append('  ');     Memo1.Append('RoundHalfDown(0.5) = '+inttostr(RoundHalfDown(0.5)));  Memo1.Append('RoundHalfDown(1.5) = '+inttostr(RoundHalfDown(1.5)));  Memo1.Append('RoundHalfDown(-0.5) = '+inttostr(RoundHalfDown(-0.5)));    Memo1.Append('RoundHalfDown(-1.5) = '+inttostr(RoundHalfDown(-1.5)));     Memo1.Append('  ');     Memo1.Append('RoundHalfDown(0.4) = '+inttostr(RoundHalfDown(0.4)));  Memo1.Append('RoundHalfDown(0.6) = '+inttostr(RoundHalfDown(0.6)));  Memo1.Append('RoundHalfDown(-0.4) = '+inttostr(RoundHalfDown(-0.4)));    Memo1.Append('RoundHalfDown(-0.6) = '+inttostr(RoundHalfDown(-0.6)));     Memo1.Append('  ');     Memo1.Append('RoundHalfToZero(0.5) = '+inttostr(RoundHalfToZero(0.5)));    Memo1.Append('RoundHalfToZero(1.5) = '+inttostr(RoundHalfToZero(1.5)));    Memo1.Append('RoundHalfToZero(-0.5) = '+inttostr(RoundHalfToZero(-0.5)));    Memo1.Append('RoundHalfToZero(-1.5) = '+inttostr(RoundHalfToZero(-1.5)));     Memo1.Append('  ');     Memo1.Append('RoundHalfToZero(0.4) = '+inttostr(RoundHalfToZero(0.4)));    Memo1.Append('RoundHalfToZero(0.6) = '+inttostr(RoundHalfToZero(0.6)));    Memo1.Append('RoundHalfToZero(-0.4) = '+inttostr(RoundHalfToZero(-0.4)));    Memo1.Append('RoundHalfToZero(-0.6) = '+inttostr(RoundHalfToZero(-0.6)));     Memo1.Append('  ');     Memo1.Append('RoundHalfToInf(0.5) = '+inttostr(RoundHalfToInf(0.5)));    Memo1.Append('RoundHalfToInf(1.5) = '+inttostr(RoundHalfToInf(1.5)));    Memo1.Append('RoundHalfToInf(-0.5) = '+inttostr(RoundHalfToInf(-0.5)));    Memo1.Append('RoundHalfToInf(-1.5) = '+inttostr(RoundHalfToInf(-1.5)));     Memo1.Append('  ');     Memo1.Append('RoundHalfToInf(0.4) = '+inttostr(RoundHalfToInf(0.4)));    Memo1.Append('RoundHalfToInf(0.6) = '+inttostr(RoundHalfToInf(0.6)));    Memo1.Append('RoundHalfToInf(-0.4) = '+inttostr(RoundHalfToInf(-0.4)));    Memo1.Append('RoundHalfToInf(-0.6) = '+inttostr(RoundHalfToInf(-0.6)));}  function RoundHalfUp(const ANumber: Float): integer;begin  If ANumber>0 then  begin    If Frac(ANumber) >= 0.5 then      result:= (Trunc(ANumber))+1    else    result:= (Trunc(ANumber));  end  else  begin    If Frac(ANumber) >= -0.5 then      result:= (Trunc(ANumber))    else      result:= (Trunc(ANumber))-1;  end;end;  function RoundHalfDown(const ANumber: Float): integer;begin  If ANumber>0 then  begin    If Frac(ANumber) <= 0.5 then      result:= (Trunc(ANumber))    else      result:= (Trunc(ANumber))+1;  end  else  begin    If Frac(ANumber) <= -0.5 then      result:= (Trunc(ANumber))-1    else      result:= (Trunc(ANumber));  end;end;  function RoundHalfToZero(const ANumber: Float): integer;begin  If ANumber>0 then  begin    If Frac(ANumber) <= 0.5 then      result:= (Trunc(ANumber))    else      result:= (Trunc(ANumber))+1;  end  else  begin    If Frac(ANumber) >= -0.5 then      result:= (Trunc(ANumber))    else      result:= (Trunc(ANumber))-1;  end;end;  function RoundHalfToInf(const ANumber: Float): integer;begin  If ANumber>0 then  begin    If Frac(ANumber) >= 0.5 then      result:= (Trunc(ANumber))+1    else      result:= (Trunc(ANumber));  end  else  begin    If Frac(ANumber) <= -0.5 then    result:= (Trunc(ANumber))-1    else      result:= (Trunc(ANumber));  end;end;  end.