2012-08-09 89 views
0

我在我的XForm中有幾個控件,讓我們稱它們爲x和y。當字段爲十進制/雙精度時,爲什麼8!= 8.000?

x的值是8; y的值是8.000 我的約束設置爲:$ X = $ y的

顯然,XForms的不考慮這些值是相等的,而不管該數據的類型是否是雙重或小數。

一種解決方案是要做到:$ X - $ Y = 0

但將XForms的有舍入誤差? 所以現在它需要的是這樣的:ABS($ X - $ Y)< 0.00001

是否有一個絕對值函數?

這看起來應該很簡單。有沒有更簡單的方法讓XForm考慮8 = 8.000?

回答

1

我不知道的語言,但在僞代碼:

設置了一些非常小$小量:= 0.00001

然後檢查$ X - $ Y $ <小量

這顯然不會捕捉所有舍入錯誤,但只要你的應用程序沒有做太重要的事情(如果是你不應該使用浮點數),這應該考慮8和8.000是一樣的。

編輯:我注意到你已經想出了這個答案,所以忽略此:)

1

您需要在比較之前先投的價值。

<xforms:input 
    ref="foo" 
    xxforms:format="format-number(xs:integer(.), '###,##0')"/> 

有關更多信息,請參見this link

0

number()解決了這個問題,並且也不需要定義數據類型。請參閱下面的簡單代碼。

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" 
    xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" 
    xmlns:xforms="http://www.w3.org/2002/xforms"> 

    <xhtml:head> 
     <xhtml:title>Xforms</xhtml:title> 

     <xforms:model> 

     <xforms:instance id="main-instance"> 
      <form> 
       <nodes> 
        <x>8</x> 
        <y>8.00</y> 
       </nodes> 
      </form> 
     </xforms:instance> 

     <xforms:bind id="x" nodeset="instance('main-instance')/nodes/x" /> 

     <xforms:bind id="y" nodeset="instance('main-instance')/nodes/y" /> 

     </xforms:model> 

    </xhtml:head> 

    <xhtml:body> 

     <table> 
      <tr> 
       <td> 
        X is 
        <xforms:output bind="x"> 
         <xforms:alert>Invalid</xforms:alert> 
        </xforms:output> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        Y is 
        <xforms:output bind="y"> 
         <xforms:alert>Invalid</xforms:alert> 
        </xforms:output> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <strong>X = Y ? </strong> 
        <xforms:output value="instance('main-instance')/nodes/x = instance('main-instance')/nodes/y"> 
         <xforms:alert>Invalid</xforms:alert> 
        </xforms:output> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <strong>(using number function) X = Y ? </strong> 
        <xforms:output value="number(instance('main-instance')/nodes/x) = number(instance('main-instance')/nodes/y)"> 
         <xforms:alert>Invalid</xforms:alert> 
        </xforms:output> 
       </td> 
      </tr> 
     </table> 

    </xhtml:body> 

</xhtml:html> 

然而,當我們給的數據類型爲的XForms:十進制到兩個節點,並且不使用數功能,這是不行的(條件返回false)。

+0

以上代碼可以在Orbeon Sandbox上運行 – Jayy 2012-08-13 06:53:13

相關問題