6 Replies Latest reply: Jul 4, 2008 1:14 PM by 807589 RSS

    RowFilter Not working for decimals

    796220
      RowFilter main_filter  = RowFilter.numberFilter(ComparisonType.EQUAL, number, sub_indices);
      RowFilter main_filter  = RowFilter.numberFilter(ComparisonType.NOT_EQUAL, number, sub_indices);
      This piece of code is not working properly for decimals(EQUAL AND NOT EQUAL).

      But it works perfectly for

      RowFilter main_filter  = RowFilter.numberFilter(ComparisonType.AFTER, number, main_indices);
      and
      RowFilter main_filter  = RowFilter.numberFilter(ComparisonType.BEFORE, number, main_indices);
      What can be the reason?
      Thanks in advance for all the valuable suggestions and replies.
      Regards,
      Anees                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: RowFilter Not working for decimals
          800456
          Without knowing what parameters you are passing, its hard to say. What exactly do yuo mean by 'decimal' in a Java context?
          • 2. Re: RowFilter Not working for decimals
            796220
            Thanks for the reply. In the codes
            RowFilter main_filter  = RowFilter.numberFilter(ComparisonType.NOT_EQUAL, number, main_indices);
            and
            RowFilter main_filter  = RowFilter.numberFilter(ComparisonType.EQUAL, number, main_indices);
            1.number is of type double
            2.int[] main_indices = new int[]{table_quality1.getModelIndex(cmbFilterColumns.getSelectedValue().toString())};
            3.double number = txtFilterNumber.getFloatValue();
            This is not working, when I give values like 1.56,10.77, 20.34....., but it works with the above code when I give 10, 20, 25....etc.

            Please help.
            Regards,
            Anees
            • 3. Re: RowFilter Not working for decimals
              796220
              I wonder why 1.00 works but not 1.45 or 2.67.
              What need to be done so that it works correctly for double values?
              Regards,
              Anees
              • 4. Re: RowFilter Not working for decimals
                807589
                Floating point numbers are not exact (1.0/3.0)*3.0 != 1, so you should avoid testing for equality. Use range comparision, truncate or, if comparing as Strings, use format.
                • 5. Re: RowFilter Not working for decimals
                  796220
                  double number = txtFilterNumber.getFloatValue();
                  getFloatValue()
                  public double getFloatValue()
                       {
                  
                            double value=0.00f;
                  
                            try
                            {
                                 value = Double.parseDouble(super.getText());
                            }
                            catch(NumberFormatException e)
                            {
                                 value = 0.00d;
                            }
                            catch(NullPointerException e)
                            {
                                 value = 0.00d;
                            }
                            return value;
                       }
                  So I need not format that number any further , right?
                  Regards,
                  Anees
                  • 6. Re: RowFilter Not working for decimals
                    807589
                    The problem is not with parsing of String. Number filter takes Number object (in this case Double) and uses it's equals method, which actually compares internal double value. That value is stored as a binary fraction. Not every decimal fraction can be exactly mapped to a finite binary fraction much less to one that fits completely into double's 64 bits. Thus you should only test if the thing falls into a sufficiently narrow range but not for equality. Filter first for values > number, then for values < number + 0.01.
                    Hope this helps.