圆数问题flex [英] Round Number problem flex

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

问题描述

这段代码有问题:

<?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)
...
我通过

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;
}

当我只点击+"或只点击-"时,我得到了正确的数字
但是如果我点击-"直到 0.7 和+",它会保持在 0.7
当我单击-"时相同的附加,直到+"时为 0.9,它保持为 0.8

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

为什么?如何将数字增加或减少 0.1?

Why? How can I do to increment or decrement a Number by 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 的情况下:

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>

或者在 Flex 4 的情况下:

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天全站免登陆