2011-08-02 6 views
2

가이 코드에 문제가있는 플렉스 :라운드 번호 문제는

<?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 (OK) 수를 클릭


1.2000000000000002 (대신 1.2)
1.3000000000000003 (1.3 대신)
1.4000000000000004 (1.4 대신)
1.5000000000000004 (1.5 대신)
(대신 1.6) 1.6000000000000005 (대신 1.7)
1.7000000000000006 (대신 1.8)
1.8000000000000007 (대신 1.9)
1.9000000000000008
...
(대신 2.0)
2.000000000000001 그리고 클릭하면 에 '-'나는 얻을
0.9 (OK)
0.8 (OK) (대신 0.7)
0.7000000000000001
0.50000 (대신 0.6)
0.6000000000000001 (대신 0.5) 00000000001 (대신 0.4)
0.40000000000000013 (대신 0.3)
0.30000000000000016 (대신 0.2)
0.20000000000000015 (대신 0.1)

0.10000000000000014 (대신 0.0)
1.3877787807814457e 16 내가 올바른 번호를

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
내가 클릭 같은 APPEND에서 유지 - '+'는 0.8

이유에 머물 때 0.9까지 ''? Number를 0.1 씩 증가 또는 감소시키는 방법은 무엇입니까?

답변

4

한번에 사용하기 :

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

또는 플렉스 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> 

또는 플렉스 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> 
+1

나는 (* 10) (내가 0.1 +)/10 Math.round가'로했다;' 를하며 감사를 작동 – Snote

0

에 체크 아웃 : 당신이 할 수있는 NumberFormatter를 사용하여 Number의 형식을 올바르게 지정하려면 100 % 확신 할 수는 없지만 float 증분을 사용하면 deci에서 일부 문제가 발생합니다. 말 점수.

<?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>