ProblemYou want to add leading or trailing zeros or spaces to a number to display it as a string. SolutionUse the custom NumberFormat class, apply a mask, and then call the format( ) method. DiscussionYou might need to format numbers with leading or trailing zeros or spaces for display purposes, such as when displaying times or dates. For example, you would want to format 6 hours and 3 minutes as 6:03 or 06:03, not 6:3. Additionally, sometimes you'll want to apply leading and/or trailing spaces to align the columns of several numbers; for example: 123456789 1234567 12345 Although you can certainly work out the algorithms on your own to add leading or trailing characters, you'll likely find working with a NumberFormat object much faster, simpler, and more flexible. The NumberFormat class is a custom class included with the downloads for this book at http://www.rightactionscript.com/ascb. That class is part of the ascb.util package, so the first thing you'll want to do is make sure you have an import statement: import ascb.util.NumberFormat; Next you need to determine the mask that you'll use to format the number. The mask can consist of zeros (0), pound signs (#), dots (.), and commas (,); any other characters are disregarded.
To better understand this, it can be helpful to take a look at some examples; consider the following mask: ##,###.0000 When the preceding mask is used with the numbers 1.2345, 12.345, 123.45, 1234.5, and 12345, the results are as follows (assuming that the localized settings apply commas as grouping symbols and dots as decimal points): 1.2345 12.3450 123.4500 1,234.5000 12,345.0000 You can set the mask for a NumberFormat object in several ways. You can specify the mask as a parameter when you construct the object, as follows: var styler:NumberFormat = new NumberFormat("##,###.0000"); Additionally, you can use the mask property of the object to change the mask at any point: styler.mask = "##.00";
Once a mask has been applied to a NumberFormat object, you can format any number value by calling the format( ) method and passing the number as a parameter: trace(styler.format(12345); The following code is a complete, working example that illustrates the features of the NumberFormat class that have been discussed so far: var styler:NumberFormat = new NumberFormat("#,###,###,###"); trace(styler.format(1)); trace(styler.format(12)); trace(styler.format(123)); trace(styler.format(1234)); styler.mask = "#,###,###,###.0000"; trace(styler.format(12345)); trace(styler.format(123456)); trace(styler.format(1234567)); trace(styler.format(12345678)); trace(styler.format(123456789)); The output from the preceding example is as follows (assuming U.S.-style localization settings): 1 12 123 1,234 12,345.0000 123,456.0000 1,234,567.0000 12,345,678.0000 123,456,789.0000 By default, NumberFormat objects attempt to automatically localize the return values. If the Flash Player is running on a U.S. English operating system, the NumberFormat class uses commas as grouping symbols and dots as decimal points. On the other hand, if the computer is running a French operating system, the symbols are reversed; dots for grouping symbols and commas for decimal points. There are several reasons why you may opt to override the automatic localization, including:
There are a variety of ways you can override the automatic localization settings:
The following example illustrates some of the ways you can override the automatic localization settings: var styler:NumberFormat = new NumberFormat("#,###,###,###.00"); Locale.slanguage = "fr"; trace(styler.format(1234)); trace(styler.format(12345, {group: ",", decimal: "."})); trace(styler.format(123456)); Locale.slanguage = "en"; trace(styler.format(1234567)); trace(styler.format(12345678, new Locale("es", "ES"))); trace(styler.format(123456789, {group: "|", decimal: ","})); The preceding code displays the following: 1.234,00 12,345.00 123.456,00 1,234,567.00 12.345.678,00 123|456|789,00 See AlsoRecipes 4.2 and 4.6 |