Formatting of numerical fields

How can I format numbers on an object table?

You can define a function to format a number as below. This function takes in an object, is names the fields that we are working with. You can optionally specify the number of digits after the decimal, the decimal char and a seperator.

/**
 * This function is applied to a number to format its display depending on different global standards
 * @param object (REQUIRED) = Journey DB object that contains the field to be formatted
 * @param field (REQUIRED) = object field to be formatted
 * @param numDigits (OPTIONAL) = number of decimals the number should have - if nothing is provided, there will be 2 digits after the decimal point by default
 * @param decimal (OPTIONAL) = symbol used before decimals - if nothing is provided a period (full stop) is used by default
 * @param seperator (OPTIONAL) = symbol used to separate every 1000 - if nothing is provided a comma is used by default
 * e.g. 1000.formatDisplay(1, '.', ',') ==> 1,000.0
 * e.g. 1000.formatDisplay(0) ==> 1,000
 * e.g. 1000.formatDisplay() ==> 1,000.00
 */
function formatNumber(object, field, numDigits, decimal, seperator) {
    decimal = decimal || ".";
    seperator = seperator || ",";
    numDigits = isNaN(numDigits = Math.abs(numDigits)) ? 2 : numDigits;

    if (!object || object[field] === undefined || object[field] === null) {
        // if null or undefined value, return 0, including specified number of digits after decimal point
        return parseInt('0').toFixed(numDigits)
    }

    var num = +object[field], // convert from string to number
        symbol = num < 0 ? "-" : "", // used for negative numbers
        str = String(parseInt(num = Math.abs(num || 0).toFixed(numDigits))), // convert number to string with decimals
        prefix = (prefix = str.length) > 3 ? prefix % 3 : 0; // calculate number of digits to preceed the first seperator

    return (
        symbol +
        (prefix ? str.substr(0, prefix) + seperator : "") // add prefix digits before first seperator
        +
        str.substr(prefix).replace(/(\d{3})(?=\d)/g, "$1" + seperator) // insert seperator before every 3 sequential digits
        +
        (numDigits ? decimal + Math.abs(num - str).toFixed(numDigits).slice(2) : "") // add decimal and numDigits if applicable
    );
}

To use this in the table or view component (any XML function):

<column heading="Start" display="{$:formatNumber($object, 'start', 0)}" />