Table of contents
No headers

NumberFormat

Last modified 18:51, 6 Nov 2014
Table of contents
No headers
/***
    USAGE: numberformat(number, format)
        formats a number to a pre-defined set of common patterns
    
    value : num
        number to format
        
    format : str
        pattern string to use; valid values are:
        '%'     : perecentage without decimals (0.1 -> 10%)
        '$'     : currency with separator for thousands and no decimals (e.g. en/us, 100 -> $100)
        'bytes' : nearest approximation in file size (100000 -> 98 kB)

***/

var value = $0 ?? $number ?? 0.0;
var format = $1 ?? $format ?? 'G';

if(format === 'bytes') {
    var factor = 0;
    if(value > 1228) {
        let factor += 1;
        let value /= 1024;
    }
    if(value > 1228) {
        let factor += 1;
        let value /= 1024;
    }
    if(value > 1228) {
        let factor += 1;
        let value /= 1024;
    }
    switch(factor) {
    default:
    case 0:
        let export = wiki.localize('System.Common.nbytes', [ num.format(value, '#,##0') ]);
    case 1:
        let export = wiki.localize('System.Common.nkbytes', [ num.format(value, '#,##0') ]);
    case 2:
        let export = wiki.localize('System.Common.nmbytes', [ num.format(value, '#,##0.#') ]);
    case 3:
        let export = wiki.localize('System.Common.ngbytes', [ num.format(value, '#,##0.##') ]);
    }
    return;
}

switch(format) {
case '%':
    let format = 'P0';
case '$':
    let format = 'C0';
}

let export = num.format(value, format);
Page statistics
184 view(s) and 6 edit(s)

Tags

This page has no custom tags.
This page has no classifications.

Comments

You must to post a comment.

Attachments