Did you know?

Some formatting options may not be so obvious. These work-arounds come from my teammate, Nick Watson, in the U.K. I thought I’d share them with you.

The CurrencyFormatter does not supply leading zeros to values less than 1. For example, when you format the value 0.45 you get $.45 and not $0.45. There is no options on the CurrencyFormatter to add or supress leading zeroes.

The work-around is to change the decimalSeparatorTo property of the CurrencyFormatter. Here’s a class which extends CurrencyFormatter and overrides the format function:

import mx.formatters.CurrencyFormatter;

public class LeadingZeroCurrencyFormatter extends CurrencyFormatter
{
public function LeadingZeroCurrencyFormatter()
{
super();
}
override public function format(value:Object):String
{
var n:Number = Number(value);
if( Math.abs(n) < 1 ) decimalSeparatorTo = "0.";
else decimalSeparatorTo = ".";

return super.format(value);
}

}

If you need to format Dates you might want to look at the DateBase class:
http://livedocs.adobe.com/flex/2/langref/mx/formatters/DateBase.html

First of all, if you need to know the names of the months or days of the week, you don’t have to code them yourself. DateBase.monthNamesLong is a static property and you can use the values in your application. Having all of the month names in one location makes it that much easier to change them.

Secondly, if you want to change the names to another language, simply replace this Array with new values:

DateBase.monthNamesShort =["Ene","Feb","Mar","Abr","May","Jun","Jul","MyMonth","Sep","Oct","Nov","Dic"];

Now any DateFormatter calls you make will use these new values for the short month names.

4 Responses to Did you know?

  1. With no doubt, Very useful Peter. Thanks!

  2. levancho says:

    very good stuf, I am bookmarking your posts,

    P.S : also I have found very useful methods in DateField Component ,
    dateToString(value:Date, outputFormat:String):String

    stringToDate(valueString:String,inputFormat:String):Date

    its not obvious to me , why these methods end up being under DateField Component and not under Date Class
    ————-
    Peter: Date is a type in the Flash Player where DataField is a Flex class and the Flex engineers wrote that code. We don’t tamper with Flash Player code.

  3. Joe says:

    What I discovered is that if you specify the currency formatter precision and use this workaround, you end up displaying two leading zeroes.

    The leading zeroes issue can be worked around by simply specifying the formatter precision. This formats a currency value with the leading zero:

    var value:Number = 0.4;
    var cf:CurrencyFormatter = new CurrencyFormatter();
    cf.precision = 2;
    return cf.format(value);

  4. Joe says:

    What I discovered is that if you specify the currency formatter precision and use this workaround, you end up displaying two leading zeroes.

    The leading zeroes issue can be worked around by simply specifying the formatter precision. This formats a currency value with the leading zero:

    var value:Number = 0.4;
    var cf:CurrencyFormatter = new CurrencyFormatter();
    cf.precision = 2;
    return cf.format(value);