Flex numeric spinner, want to add preceeding 0

Go To StackoverFlow.com

2

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?

2012-04-05 02:31
by fred basset


7

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>
2012-04-05 04:41
by Jason Sturges
Thank you, excellent answe - fred basset 2012-04-07 16:28
Ads