Announcement

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

    Bug with toCurrencyString() with negative numbers

    Number.toCurrencyString() has a bug when using negative numbers.

    Steps to reproduce:

    Code:
    n = parseFloat("22.99");
    n.toCurrencyString('$');  // outputs "$22.99" as expected
    
    n2 = parseFloat("-22.99");
    n2.toCurrencyString('$'); // outputs "$-23.01"  OOPS!
    This is using the nightly build of SmartClient for 2011-12-26. Although it happens in earlier versions, I just grabbed the latest to ensure it was still a problem.

    Chris

    #2
    Bump.

    Is it just me that thinks this is a major problem when displaying financial information in a ListGrid?

    Here is an example of the problem:
    Code:
    var testData = [
    {pk:0, orderID:8805, itemDescription:"Anti Virus Suite",
        category:"Software", shipDate:new Date(2009,10,20), quantity:10 , unitPrice:-50.99 },
    {pk:1, orderID:4833, itemDescription:"USB Ergonomic Keyboard",
        category:"Hardware", shipDate:new Date(2009,10,13), quantity:10 , unitPrice:50.99 }
    ];
    
    isc.DataSource.create({
        ID:"orderItemLocalDS",
        clientOnly:true,
        testData:testData,
        fields:[
            {name:"pk", type:"sequence", primaryKey:"true", hidden:"true"},
            {name:"orderID", type:"integer", canEdit:false, title:"Order ID", pluralTitle:"Orders"},
            {name:"itemDescription", type:"text", title:"Description"},
            {name:"category", type:"text", title:"Category"},
            {name:"shipDate", type:"date", title:"Ship Date"},
            {name:"quantity", type:"integer", title:"Quantity"},
            {name:"unitPrice", type:"float", title:"Price"}
        ]
    });
    isc.ListGrid.create({
        ID: "companyList",
        width:600, height:520,
        alternateRecordStyles:true,
        autoFetchData:true,
        dataSource:orderItemLocalDS,
        showAllRecords:true,
        groupByField:"category", groupStartOpen:"all",
        canEdit:true, editEvent:"click",
        
        showGridSummary:true,
        showGroupSummary:true,
        fields:[
            {name:"orderID", includeInRecordSummary:false, summaryFunction:"count"},
            {name:"itemDescription"},
            {name:"category", showGridSummary:true, 
                getGridSummary:function (records, summaryField) {
                    var seenCategories = {};
                    for (var i = 0; i < records.length; i++) {
                        seenCategories[records[i].category] = true;
                    }
                    var totalCategories = isc.getKeys(seenCategories).length;
                    return totalCategories + " Categories";
                    
                }
            },
            {name:"shipDate", showGroupSummary:true, showGridSummary:false, summaryFunction:"max"},
            
            {name:"quantity", showGroupSummary:false, showGridSummary:false},
            {name:"unitPrice", showGroupSummary:false, showGridSummary:false,
             formatCellValue:function (value) {
                 if (isc.isA.Number(value)) {
                    return value.toCurrencyString("$");
                 }
                 return value;
             }
            },
            {name:"Total", type:"summary", recordSummaryFunction:"multiplier",
             summaryFunction:"sum",
             showGridSummary:true, showGroupSummary:true,
             align:"right",
             formatCellValue:function (value) {
                 if (isc.isA.Number(value)) {
                    return value.toCurrencyString("$");
                 }
                 return value;
             }
            }
        ]
    })
    I took this code straight from here:
    http://www.smartclient.com/#summaryGrid

    The only difference is that I cut out a bunch of the products to make it more readable, and changed the product prices and quantities to reflect the problem.

    You will see that both products are 50.99 in the data source. The only difference being that one of them is a negative value. (Obviously a negative product value doesn't make sense, but for my application negative numbers are valid entries.) Take for example a grid showing order details from an order where there was a return which generated a negative amount. Or a balance sheet showing the quarterly results. Negative numbers will exist in the grid.

    In this example the 'Anti Virus Suite Software' is displayed as $-51.01 even though it is -50.99 in the DataSource.

    You will note that the summary total at the bottom of the grid shows the appropriate value of 0.00 since it is acting on the actual values rather than the displayed values.

    You will also notice that the 'Total' column magnifies the error by using the displayed value rather than the real value for its calculation.

    I can't believe that I'm the only person that has a problem with this.

    Comment


      #3
      This has been fixed (all active branches). Thanks for the report.

      Comment


        #4
        toCurrencyString() still not working correctly

        This is better but not completely fixed yet.

        I got the same problem as "reaper" and updated to version 8.2 build 2012-10-22. This solved only one part of the problem : negative values are not tranformed into other values anymore, but sometimes the - sign is omitted. Quite annoying ...

        You can try with:

        var value=-0.05;
        var formattedVal = value.toCurrencyString('%','.',true,true);

        I got the same problem with -0.37 and -0.06, but not with -8.59 nor -2.87.

        Comment


          #5
          I can confirm this issue. It's not fixed for negative numbers > -1.

          Jochen

          Comment


            #6
            This new issue is also fixed as of last week in 2.5p, 3.0p and 3.1d builds.

            Comment

            Working...
            X