我对这段代码有疑问:
<?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}"/>
当我点击“+”时我得到
1.1(好的)
1.2000000000000002(而不是 1.2)
1.3000000000000003(而不是 1.3)
1.4000000000000004(而不是 1.4)
1.5000000000000004(而不是 1.5)
1.6000000000000005(而不是 1.6)
1.7000000000000006(而不是 1.7)
1.8000000000000007(而不是 1.8)
1.9000000000000008(而不是 1.9)
2.000000000000001(而不是 2.0)
...
当我点击“-”时我得到了
0.9(还可以)
0.8(还可以)
0.7000000000000001(而不是 0.7)
0.6000000000000001(而不是 0.6)
0.5000000000000001(而不是 0.5)
0.40000000000000013(而不是 0.4)
0.30000000000000016(而不是 0.3)
0.20000000000000015(而不是 0.2)
0.10000000000000014(而不是0.1)
1.3877787807814457e-16(而不是0.0)
...
我改变我的功能
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;
}
当我仅单击“+”或仅单击“-”时,我会得到正确的数字
但如果我点击“-”直到 0.7,然后点击“+”,它就会保持在 0.7
当我单击“-”时相同的附加,直到 0.9,当“+”时,它保持为 0.8
为什么?如何将数字增加或减少 0.1?
尝试使用:
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;
}
或者对于 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>
或者对于 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)