We recently created a number function that automatically ensures the number_format is no longer than 4 digits long and has the relevant unit (e.g 41.75K) written after. Its main application would be in data analytics but it can be used anywhere numbers are presented on the front end with Couch. To install, add the code below in couch/addons/kfunctions.php.

Code: Select all
$FUNCS->add_event_listener( 'alter_tag_number_format_execute', array('CustomNumberFormatTag', 'custom_number_format_tag') );
class CustomNumberFormatTag{
    static function custom_number_format_tag( $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'=>',',
                          'unit_recognition'=>'0'       /* boolean variable, off by default*/
                          ),
                    $params)
                );
        $decimal_precision = trim( $decimal_precision );
        if( !is_numeric($decimal_precision) ) $decimal_precision = 2;
        $decimal_character = trim( $decimal_character );
        $unit_recognition = trim( $unit_recognition );
        if ($unit_recognition == 1) {
            if ($number >= 1000000){
                $number = $number / 1000000;
                $decimal_precision = 2;
                $unit = "M";
            } elseif ($number >= 1000) {
                $number = $number / 1000;
                $decimal_precision = 2;
                $unit = "K";
            } else{
                $unit;
                $decimal_precision = 0;
            }
           
        }
       
        if ($number < 1 ){
            $html = 0;
        } else{
            $html = number_format((float) $number, $decimal_precision, $decimal_character, $thousands_separator ) . $unit;
        }
        return 1;
    }
}


To enable in your code add 'unit_recognition='1' to the <cms:number_format/> tag e.g <cms:number_format "<cms:show k_total_records/>" unit_recognition='1'/>