Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DataSourceFloatField in a ListGrid

    Firefox 3.6 / SmartGWT 2.4

    I load a DataSourceFloatField with float value (15.15), but when displayed in a ListGrid, it shows up as (15.149999618530273). Looks like its using getAttribute() instead of getAttributeAsFloat().

    Tried a CellFormatter, but the problem occurs in edit mode and when displayed in a pop-up.

    Any ideas on how to fix this?

    #2
    The question is how you are providing the data. Try putting together a standalone test case and you'll most likely find that the data is getting into a bad state somewhere before the ListGrid sees it.

    Comment


      #3
      ListGridRecord record = new ListGridRecord();
      record.setAttribute("freq", 15.15f);

      System.out.println("freq1: " + record.getAttribute("freq"));
      System.out.println("freq2: " + record.getAttributeAsFloat("freq"));

      OUTPUT:

      freq1: 15.149999618530273
      freq2: 15.15

      Comment


        #4
        In Javascript only 64 bit floating point numeric types are available so the code

        Code:
        record.setAttribute("freq", 15.15f);
        System.out.println("freq1: " + record.getAttribute("freq"));
        is question is equivalent to

        float -- > double --> toString()

        Since floating point numbers are represented as base 2 decimal numbers, when many users use the float type, it is often not understood that the number is actually not what they think it is (except for cases like 0.5 where the number can be exactly represented as a binary decimal). Casting to double and subsequently calling toString() on it merely shows the extra precision that was already present.

        This is analogous to the following (non-GWT) Java code :

        String strVal = String.valueOf((double)15.15f);

        Here strVal will be "15.149999618530273".

        In short, to avoid such confusion, always use the type "double". Eg

        Code:
        record.setAttribute("freq", 15.15d);
        System.out.println("freq1: " + record.getAttribute("freq"));
        Although the class name is DataSourceFloatField, it merely indicates it’s a floating point field and is not limited to use of "float" types.

        Sanjiv

        Comment

        Working...
        X