回合数问题flex [英] Round Number problem flex

查看:140
本文介绍了回合数问题flex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码有问题:

 <?xml version =1.0encoding =utf-8 >?; 
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx
showStatusBar =false
width =250height =31>
< s:layout>
paddingTop =10verticalAlign =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>



当我点击'+ '我得到

1.1(OK)

1.2000000000000002(而不是1.2)

1.3000000000000003(而不是1.3)

1.4000000000000004 (而不是1.5)

1.5000000000000004(而不是1.5)

1.8000000000000005(而不是1.6)

1.7000000000000006(而不是1.7)

1.8000000000000007 (而不是1.8)

1.9000000000000008(而不是1.9)

2.000000000000001(而不是2.0)

...

而当我点击' - '我得到

0.9(OK)

0.8(OK)

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)

...

我改变我的f (b:







$ e.currentTarget == plus)
i = Math.floor((i + 0.1)* 10)/ 10; (e.currentTarget == minus)
i = Math.floor((i - 0.1)* 10)/ 10;
}

当我点击+或者只点击' - '

但是如果我点击' - '直到0.7和'+',它会停留在0.7

点击' - ' ,它保持到0.8

为什么?我怎样才能以0.1递增或递减数字?

解决方案

尝试使用:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ((i + 0.1)×10)/ 10; (e.currentTarget == minus)
i = Math.round((i - 0.1)* 10)/ 10;
if
}

或者在Flex 4.5中:

 <?xml version =1.0encoding =utf-8?> 
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx
showStatusBar =false
width =250height =31>
< s:layout>
paddingTop =10verticalAlign =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:声明>
< / fx:声明>< s:Button id =pluswidth =30label =+click =click(event)/>
< / s:WindowedApplication>

或者在Flex 4中:

 <?xml version =1.0encoding =utf-8?> 
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx
showStatusBar =false
width =250height =31>
< s:layout>
paddingTop =10verticalAlign =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:声明>
< / fx:声明>< s:Button id =pluswidth =30label =+click =click(event)/>
< / s:WindowedApplication>


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?

解决方案

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>

这篇关于回合数问题flex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆