Formatting values in an FxNumericStepper control using a custom display format function in Flex Gumbo

The following example shows how you can format the value displayed in a Flex Gumbo FxNumericStepper control by setting a custom display format function using the displayFormatFunction property.

The displayFormatFunction property was added in build 4228. Download Adobe Flex Gumbo SDK.

Full code after the jump.

According to the [beta] documentation on the dataFormatFunction property:

Callback function that formats the value displayed in the text input field. The function takes a single Number as an argument and returns a formatted String.

The function has the following signature:

funcName(value:Number):String

The default value is undefined.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/04/formatting-values-in-an-fxnumericstepper-control-using-a-custom-display-format-function-in-flex-gumbo/ -->
<Application name="FxNumericStepper_displayFormatFunction_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        <![CDATA[
            private function numericStepper_dispFmtFunc(value:Number):String {
                return numFormatter.format(value);
            }
        ]]>
    </Script>

    <Declarations>
        <NumberFormatter id="numFormatter" precision="2" />
    </Declarations>

    <!-- The displayFormatFunction property was added in build 4228. -->
    <FxNumericStepper id="numericStepper"
            minimum="0"
            maximum="10"
            value="5"
            stepSize="0.01"
            valueInterval="0.01"
            displayFormatFunction="numericStepper_dispFmtFunc" />

</Application>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/04/formatting-values-in-an-fxnumericstepper-control-using-a-custom-display-format-function-in-flex-gumbo/ -->
<Application name="FxNumericStepper_displayFormatFunction_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <Script>
        <![CDATA[
            import mx.components.FxNumericStepper;
            import mx.formatters.NumberFormatter;

            private var numFormatter:NumberFormatter;
            private var numericStepper:FxNumericStepper;

            private function init():void {
                numFormatter = new NumberFormatter();
                numFormatter.precision = 2;

                numericStepper = new FxNumericStepper();
                numericStepper.minimum = 0;
                numericStepper.maximum = 10;
                numericStepper.value = 5;
                numericStepper.stepSize = 0.01;
                numericStepper.valueInterval = 0.01;
                /* The displayFormatFunction property was added in build 4228. */
                numericStepper.displayFormatFunction = numericStepper_dispFmtFunc;
                addChild(numericStepper);
            }

            private function numericStepper_dispFmtFunc(value:Number):String {
                return numFormatter.format(value);
            }
        ]]>
    </Script>

</Application>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.

4 thoughts on “Formatting values in an FxNumericStepper control using a custom display format function in Flex Gumbo

  1. Hi Peter,
    can you please tell me, why this example with thousands doesn’t work?

    Kind Regards

    <?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” minWidth=”955″ minHeight=”600″>
    <fx:Script>
    <![CDATA[
    private function numericStepper_dispFmtFunc(value:Number):String {
    return numFormatter.format(value);
    }

    ]]>
    </fx:Script>

    <fx:Declarations>
    <mx:NumberFormatter id=”numFormatter” precision=”2″ />
    </fx:Declarations>

    <s:NumericStepper id=”numericStepper”
    minimum=”10000″
    maximum=”20000″
    value=”15000″
    stepSize=”10000″
    valueFormatFunction=”numericStepper_dispFmtFunc” />

    </s:Application>

Comments are closed.