[AS3] Introducing the OddFormatter class

by Bernd on January 28th, 2010

If you need a formatter that supports the European, British and even American odd format, have a look at the following piece of code. If you have no idea about the different formats you might check the Odd Article on Wikipedia first.

Please note that for the UK format, the odd is always converted to the lowest possible term. Wikipedia says that 3/2 is usually represented as 6/4, 10/3 is 100/30, but I’m pretty sure that this is not commonly used, at least for the online bookmakers where the odds are automatically converted in the different formats.

You can also download the source here.

As usual, this code is free, use it wherever you want ;)

package com.firstrowria.formatters
{
   public class OddFormatter
   {
      public static const EU_FORMAT:uint = 0;
      public static const US_FORMAT:uint = 1;
      public static const UK_FORMAT:uint = 2;

      public static function format(v:Number, f:uint = 0, c:String = “,”, p:int = 2):String
      {
         var formattedValue:String = “”;

         if (f == UK_FORMAT)
         {
            var hundretValue:Number = Math.round(v * 100) – 100;
            var ggt:int = ggt(hundretValue, 100);
            formattedValue = (hundretValue / ggt) + “/” + (100 / ggt);
         }
         else if (f == US_FORMAT)
         {
            if (v >= 2)
               formattedValue = “+” + Math.round(100 * (v – 1));
            else if (v == 1)
               formattedValue = “-0″;
            else
               formattedValue = “” + Math.round(100 / (1 – v));
         }
         else if (f == EU_FORMAT)
         {
            formattedValue = v.toFixed(p)
            if (c != “.”)
               formattedValue = formattedValue.replace(”.”, c);
         }

         return formattedValue;
      }

      private static function ggt(u:int, v:int):int
      {
         return ((u > 0) ? ggt(v % u, u) : v);
      }
   }
}

1 Comment
  1. biggrant permalink

    I know it sounds crazy but all the high street bookies in the UK do use 100/30 and 6/4 etc, for the lowest betting, not sure why and doesn’t make much difference to the soccer app, and I’m not stalking you lol

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS