简体   繁体   中英

Why are my numeric stepper values so far out past the decimal point?

I'm using the Flex 4.6 Spark Numeric Stepper in my app and when I enter .9 it returns, "0.9000000953674316". I can enter any value actually. It is doing it for all of them.

If I use the arrow buttons it moves from 0 to 1 it sets the value to "0.09999990463256836".

UPDATE:
So running more tests. If I start and zero and then go down it's:

0
-0.1
-0.2
-0.30000000000000004
-0.4

and if I then go back up it's:

-0.30000000000000004
-0.20000000000000004
-0.10000000000000003
-2.7755575615628914e-17
NaN

Here is my formatter code:

precision = 100;
public function formatNumericStepper(value:Number):String {
    return String(int(value*precision)/precision);
}

Note: Sometimes the value gets stuck at -.7 or .28. It works fine for whole numbers but it's buggy as heck with numbers less than one. Or I'm doing something wrong.

With some finessing and helpful direction from @DodgerThud the following seems to be working:

MXML:

<s:NumericStepper id="numericStepper" snapInterval="0" stepSize=".01"
                  valueFormatFunction="formatNumericStepper"
                  valueParseFunction="valueParseFunctionNew"/>

ACTIONSCRIPT:

public var precision:int = 100;

public var fixedPosition:int = 2;

/**
 * Format numeric stepper
 * Trim down to 2 decimal places.
 * */
public function formatNumericStepper(value:Number):String {
    var out:String;
    if (fixedPosition!=0) {
        out = Number(value.toFixed(fixedPosition)).toString();
    }
    else {
        out = String(int(value * precision) / precision);
    }
    return out;
}


public function valueParseFunction(value:String):Number {
    var out:Number;
    if (fixedPosition!=0) {
        out = Number(Number(value).toFixed(fixedPosition));
    }
    else {
        out = Number(value);
    }
    return out;
}

I think it was getting stuck because if you have a small enough step size, say .1 and then you are rounding using value*100/100 you are sometimes rounding down. Therefore, it is never a large or small enough step to get past a certain value.

The else statements could probably be removed. I kept them in so if it's possible to do it dynamically I can use the step size to get the decimal position as a counter.

So if step size is .1 then fixedPosition would be 1. If step size is .01 then fixedPosition would be 2. If .001 or .999 then the fixed position would be 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM