2012-08-09 119 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