Round Number problem flex

2019-02-26 05:20发布

问题:

I have a problem with this code :

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   showStatusBar="false"
                   width="250" height="31">
<s:layout>
    <s:HorizontalLayout gap="10" paddingBottom="10" paddingLeft="10" paddingRight="10"
                        paddingTop="10" verticalAlign="middle"/>
</s:layout>
<fx:Script>
    <![CDATA[
        [Bindable]
        private var i:Number = 1.0;

        private function click(e:MouseEvent):void
        {
            if (e.currentTarget == plus)
                i += .1;
            if (e.currentTarget == minus)
                i -= .1;
        }
    ]]>
</fx:Script>
<s:Button id="plus" width="30" label="+" click="click(event)"/>
<s:Button id="minus" width="30" label="-" click="click(event)"/>
<s:Label text="{i}"/>

When I click on '+' I get
1.1 (OK)
1.2000000000000002 (instead of 1.2)
1.3000000000000003 (instead of 1.3)
1.4000000000000004 (instead of 1.4)
1.5000000000000004 (instead of 1.5)
1.6000000000000005 (instead of 1.6)
1.7000000000000006 (instead of 1.7)
1.8000000000000007 (instead of 1.8)
1.9000000000000008 (instead of 1.9)
2.000000000000001 (instead of 2.0)
...
And when I click on '-' I get
0.9 (OK)
0.8 (OK)
0.7000000000000001 (instead of 0.7)
0.6000000000000001 (instead of 0.6)
0.5000000000000001 (instead of 0.5)
0.40000000000000013 (instead of 0.4)
0.30000000000000016 (instead of 0.3)
0.20000000000000015 (instead of 0.2)
0.10000000000000014 (instead of 0.1)
1.3877787807814457e-16 (instead of 0.0)
...
I change my function by

private function click(e:MouseEvent):void
{
    if (e.currentTarget == plus)
        i = Math.floor((i + 0.1) * 10) / 10;
    if (e.currentTarget == minus)
        i = Math.floor((i - 0.1) * 10) / 10;
}

I get the right number when I click on only '+' or only on '-'
But if I click '-' until 0.7 and '+', it stays at 0.7
The same append when I click '-' until 0.9 when '+', it stay to 0.8

Why? How can I do to increment or decrement a Number by 0.1?

回答1:

Try to use:

private function click(e:MouseEvent):void
{
    if (e.currentTarget == plus)
        i = Math.round((i + 0.1) * 10) / 10;
    if (e.currentTarget == minus)
        i = Math.round((i - 0.1) * 10) / 10;
}

Or in case of Flex 4.5:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   showStatusBar="false"
                   width="250" height="31">
<s:layout>
    <s:HorizontalLayout gap="10" paddingBottom="10" paddingLeft="10" paddingRight="10"
                        paddingTop="10" verticalAlign="middle"/>
</s:layout>
<fx:Script>
    <![CDATA[
        [Bindable]
        private var i:Number = 1.0;

        private function click(e:MouseEvent):void
        {
            if (e.currentTarget == plus)
                i += .1;
            if (e.currentTarget == minus)
                i -= .1;
        }
    ]]>
</fx:Script>
<fx:Declarations>
    <s:NumberFormatter id="numberFormatter" trailingZeros="true" fractionalDigits="1" />
</fx:Declarations><s:Button id="plus" width="30" label="+" click="click(event)"/>
<s:Button id="minus" width="30" label="-" click="click(event)"/>
<s:Label text="{numberFormatter.format(i)}"/>
</s:WindowedApplication>

Or in case of Flex 4:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   showStatusBar="false"
                   width="250" height="31">
<s:layout>
    <s:HorizontalLayout gap="10" paddingBottom="10" paddingLeft="10" paddingRight="10"
                        paddingTop="10" verticalAlign="middle"/>
</s:layout>
<fx:Script>
    <![CDATA[
        [Bindable]
        private var i:Number = 1.0;

        private function click(e:MouseEvent):void
        {
            if (e.currentTarget == plus)
                i += .1;
            if (e.currentTarget == minus)
                i -= .1;
        }
    ]]>
</fx:Script>
<fx:Declarations>
    <mx:NumberFormatter id="numberFormatter" precision="1" rounding="nearest" />
</fx:Declarations><s:Button id="plus" width="30" label="+" click="click(event)"/>
<s:Button id="minus" width="30" label="-" click="click(event)"/>
<s:Label text="{numberFormatter.format(i)}"/>
</s:WindowedApplication>


回答2:

Check this out: You can use NumberFormatter to properly format your Number, I am not 100% sure but using float increments creates some problem at the decimal points.

<?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">
<s:layout>  
    <s:HorizontalLayout gap="10" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10" verticalAlign="middle"/>
</s:layout>
<fx:Script>    
    <![CDATA[   
        [Bindable]   
        private var i:Number = 1.0;   
        private function click(e:MouseEvent):void    
        {          
            if (e.currentTarget == plus)   
                i += 0.1;          
            if (e.currentTarget == minus)   
                i -= 0.1;    
        }    
    ]]> 
</fx:Script>
<fx:Declarations>
    <mx:NumberFormatter id="numFormatter" precision="2" rounding="nearest"/>    
</fx:Declarations>

<s:Button id="plus" width="30" label="+" click="click(event)"/> 
<s:Button id="minus" width="30" label="-" click="click(event)"/> 
<s:Label text="{numFormatter.format(i)}"/>
</s:Application>


标签: flex