Plugin function "Round()" doesn't work properly

Post your questions and problem reports here

Moderator: kfury77

Forum rules
Please try to follow these guidelines. This will help to receive faster and more accurate response.

  • Check the Support section of the corresponding product first. Chances are you will find your answer there;
  • Do not create new topics for already reported problems. Add your comments to the existing topics instead;
  • Create separate topic for each problem request. Do NOT post a number of non-related problem reports in a single topic;
  • Give your topic a meaningful title. Titles such as "A question," "Bug report" and "Help!" provide others no clue what your message is about;
  • Include the version number of the software you are using;
  • This is not an official customer support helpdesk. If you need a prompt and official response, please contact our support team directly instead. It may take a while until you receive a reply in the forum;

Plugin function "Round()" doesn't work properly

Postby pmk65 » Sat May 23, 2015 2:06 am

Im working on a plugin and noticed that the calculations made differed from the ones I get from regular Javascript.
After a lot of debugging, I pinpointed the problem to the "Round()" function.

Here's an example of totally inconstant results (WeBuilder Plugin script):

Code: Select all
var test1 = Round(11.5); // Returns 12
var test2 = Round(10.5); // Returns 10 << SHOULD BE 11

Script.ClearMessages;
Script.Message(_t(test1));
Script.Message(_t(test2) + " WRONG! Should be 11");


Why does one number round UP but the other rounds DOWN, even if both have the exact same fraction (.5) ? :?:

Program used: WeBuilder 2015 v13.2.0.164
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Plugin function "Round()" doesn't work properly

Postby pmk65 » Sat May 23, 2015 2:29 pm

Turns out that Delphi/Webuilder uses a retarded form of rounding called "banker's rounding"! :shock:

Round y to an integer value, call it q.
If the fraction of y is 0.5, then q is the even integer nearest to y.



Here's a custom rounding function I made, that behaves as you expect a rounding function to work. ;)

Code: Select all
   /**
    *
    * Helper function: Round float var to specific number of decimals
    *
    * @param     float   floatVar: The number to round
    * @param     int     decimalPlaces: Number of decimals
    * @return    float
    *
    */
   function RoundFloat(floatVar, decimalPlaces) {
      var p = StrToInt(Copy("1000000000", 1, decimalPlaces + 1)),
         t = floatVar * p,
         r = Trunc(t);
      if (Frac(t) >= 0.5) r = r + 1;
      return (r / p);
   }
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark


Return to HTMLPad / Rapid CSS / Rapid PHP / WeBuilder Support

Who is online

Users browsing this forum: No registered users and 32 guests

cron