actionscript-3apache-flexadobeflex4.5flexbuilder

Flex numeric spinner, want to add preceeding 0


I have a Flex UI and want the numeric stepper to add a preceeding '0' to the displayed value if it's between 0 and 9, so that it's always 2 digits. How do I do this?


Solution

  • Use the valueFormatFunction of the NumericStepper:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx">
    
        <fx:Script>
            <![CDATA[
                protected function formatNumber(value:Number):String
                {
                    if (value < 10)
                        return '0' + value;
    
                    return value.toString();
                }
            ]]>
        </fx:Script>
    
        <s:NumericStepper valueFormatFunction="formatNumber"
                          minimum="0"
                          maximum="100" />
    
    </s:Application>