Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
2 posts Page 1 of 1
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;
}

Thanks Anton.

Explicitly converting the number to float, as being done in your code, can result in unexpected results sometimes e.g.
Code: Select all
<cms:number_format '510.9' /> // outputs 510.89

I have tried to make some modifications. Could you please test and let me know if it gives the desired results?
Code: Select all
$FUNCS->add_event_listener( 'alter_tag_number_format_execute', 'my_tag_mod_number_format');
function my_tag_mod_number_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'=>',',
                      'round'=>'1'
                      ),
                $params)
           );
    $number = trim( $number );
    $decimal_precision = trim( $decimal_precision );
    if( !$FUNCS->is_natural($decimal_precision) ) $decimal_precision = 2;
    $decimal_character = trim( $decimal_character );
    $round = ( $round==0 ) ? 0 : 1;

    // prevent rounding
    if( !$round ){
        $pos = strpos( $number, '.' );
        if( $pos!==false ){ $number = substr( $number, 0, $pos+1+$decimal_precision ); }
    }

    // perform formatting
    $html = number_format( (float)$number, $decimal_precision, $decimal_character, $thousands_separator );

    return true;
}

Example usage
Code: Select all
<cms:number_format '21.188' /> // outputs 21.19
<cms:number_format '21.188' round='0' /> // outputs 21.18

I have deliberately preserved the default behaviour of rounding to preserve backward compatibility and to keep fidelity with how PHP function behaves.

Please let me know and we can add this to the core.
Thanks.
2 posts Page 1 of 1