Hello,

Code below goes to /couch/addons/kfunctions.php and allows to use "cms:number_format" tag as usual, but its results will never be rounded, e.g.: "3.66" with decimal_precision='1' will become "3.6" and not "3.7".

Code: Select all

// Make number_format actually format and not round.
$FUNCS->add_event_listener( 'alter_tag_number_format_execute', 'precise_format');
function precise_format( $tag_name, $params, $node, &$html ){
    global $FUNCS;

    if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}

    extract( $FUNCS->get_named_vars(
                array(
                      'number'=>'',
                      'decimal_precision'=>'2', /* default 2 digit after decimal point */
                      'decimal_character'=>'.', /* char used to denote decimal */
                      'thousands_separator'=>','
                      ),
                $params)
           );
    $number = trim( $number );
    $decimal_precision = trim( $decimal_precision );
    if( !is_numeric($decimal_precision) ) $decimal_precision = 2;
    $decimal_character = trim( $decimal_character );

    // perform trimming
    $number = floor( (float)$number ).substr( $number - floor($number), 1, $decimal_precision + 1);
    // perform formatting
    $html = number_format( (float)$number, $decimal_precision, $decimal_character, $thousands_separator );

    return 1;
}