Results 1 to 4 of 4
  1. #1
    Member
    Join Date
    Feb 2010
    Location
    London
    Posts
    3

    Modifying Decimal Places Displayed

    Hi,

    I have created a page that displays foreign exchange rates with 6 decimal places, such as GBP 1.654321 and I want to display only 4. The code that Iíve added is shown below, but it does not have any effect on the resulting display.

    Thanks.

    //added to format decimal places
    pushtable.onChangingValues = formatValues;

    //copied from StockListDemo
    //WDX is name of field in schema that requires formatting
    function formatValues(item, itemUpdate) {
    if (itemUpdate == null) return;
    var newValue = itemUpdate.getFormattedValue("WDX");
    if (newValue != null) {
    var formattedVal = formatDecimal(newValue, 4, true);
    itemUpdate.setFormattedValue("WDX",formattedVal);
    }
    }


    // format a decimal number to a fixed number of decimals
    function formatDecimal(value, decimals, keepZero) {
    var mul = new String("1");
    var zero = new String("0");
    for (var i = decimals; i > 0; i--) {
    mul += zero;
    }
    value = Math.round(value * mul);
    value = value / mul;
    var strVal = new String(value);
    if (!keepZero) {
    return strVal;
    }

    var nowDecimals = 0;
    var dot = strVal.indexOf(".");
    if (dot == -1) {
    strVal += ".";
    } else {
    nowDecimals = strVal.length - dot - 1;
    }
    for (var i = nowDecimals; i < decimals; i++) {
    strVal = strVal + zero;
    }

    return strVal;
    }

  2. #2
    Member
    Join Date
    Feb 2010
    Location
    London
    Posts
    3
    I have just discovered that the code for the decimal formatting is working. However, the code below appears to void the formatting. Any suggestions.

    pushtable.onChangingValues = function(itemPos, visualUpdateInfo, itemName) {
    function formatHighlites(itemPos, visualUpdateInfo, itemName) {
    if (visualUpdateInfo != null) {
    var cold = (itemPos % 2 == 1) ? "#eeeeee" : "#ddddee";
    visualUpdateInfo.setRowAttribute("lightblue", cold, "backgroundColor");
    }
    }

  3. #3
    Power Member
    Join Date
    Jul 2006
    Location
    Cesano Maderno, Italy
    Posts
    784
    Hi,

    if there are no typos in your second message, you don't execute anything in your handler, you just declare a function there and you also miss a }
    Moreover in the second post there is no call to the formatDecimal method.
    Finally, if the code in both posts is in the same page, you're writing the onChangingValues callback two times, so that the first assignment is overwritten by the second one

    Probably your onChangingValues assignment should look like this:

  4. #4
    Member
    Join Date
    Feb 2010
    Location
    London
    Posts
    3
    Hi,

    Thank-you for the prompt reply. I realised, through further testing last night, that the second onChangingValue call was overwritting the first. Then I struggled with creating the call that you have provided. This works perfectly and this thread is closed.

    Thanks, Mike

 

 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +1. The time now is 07:21 PM.