0 Replies Latest reply on Sep 19, 2007 1:48 PM by Handycam

    Fraction formatter

    Handycam Level 1
      I made a FractionFormatter like so:


      package myComponents
      //Import base Formatter class
      import mx.formatters.Formatter
      public class FractionFormatter extends Formatter {

      // Declare the variable to hold the pattern string.
      public var myNumber:String;

      // Constructor
      public function FractionFormatter() {
      // Call base class constructor.

      // Override format().
      override public function format(value:Object):String {
      //Validate value - it must be a nonzero length string.
      if(value.length == 0) {
      // Return empty string and error message for zero-length string.
      error="Zero Length String";
      return ""

      //If the value is valid, format the string.
      switch (myNumber) {
      case ".25" :
      return "1/4";
      case ".5":
      return "1/2";
      case ".75":
      return "3/4";
      case "1":
      return "1";
      case "1.25":
      return "1 1/4";

      default :
      // If formatString is not "upper" or "lower",
      // return empty string and set the error message.
      error="Invalid Format String";
      return "";

      It works fine if I use it one something like a text field:
      <comp:FractionFormatter id="makeFraction" myNumber="{0}"/>
      <mx:TextInput id="myTI"/>
      <mx:TextArea text="Formatted string is {makeFraction.format(myTI.text)}"/>

      But I need to use on a datagrid cell, and this is not working:
      <mx:DataGridColumn dataField="{makeFraction.format(data)}" headerText="Amount" width="40"/>

      Here is the data grid column without the formatter, which works fine:
      <mx:DataGridColumn dataField="0" headerText="Amount" width="40"/>

      Any ideas, please?