0

is there any way to replace this:

<div>{math equation="x - y" x={$n.total_rating} y={$n.total_ratings}}</div>

Where results is for example 1300 to receive this 1.3k, and so on...

  • this question is already asked: https://stackoverflow.com/questions/9461621/format-a-number-as-2-5k-if-a-thousand-or-more-otherwise-900 – w3_ Oct 10 '21 at 18:03

1 Answers1

0

Try in libs\plugins\modifier.num_format.php

<?php
/**
 * Smarty plugin
 *
 * @package    Smarty
 * @subpackage PluginsModifier
 */

function smarty_modifier_num_format($string) {
  if($string>1000) {
  $x = round($string);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return $x_display;
  }
  return $string;
}

and in theme file

{assign var="mynubers" value="{math equation="x - y" x={$n.total_rating} y={$n.total_ratings}}"}
{if $mynubers > '1000'}{$mynubers|num_format}{else}{$mynubers}{/if}
Peca
  • 1,892
  • 3
  • 20
  • 21