Can I use the string $ string number_format for a superscript of decimal places?

I would like to make decimals in the following string representation as superscript:

$string .= number_format(round($value, (int)$decimal_place), (int)$decimal_place, $decimal_point, $thousand_point);

I am not very smart, but since I tried to use different methods, I found stack overflow. Things like ."<sup>or simple "<sup>", as well as .'<sup>'so many other combinations, but nothing works. I either get an error, or the price disappears due to an error that I introduced into the code, because, as I said, I'm not the sharpest tool in the barn.

+3
source share
2 answers

, , , ,

<?
function superscript_value($value, $prefix = '$') {
    $decimal_place = 2;
    $decimal_point = '.';
    $thousand_point = ',';

    if(round($value, 0) == $value)
        return $prefix . $value;
    else
        return $prefix . preg_replace("/\.(\d*)/", "<sup>.$1</sup>", number_format($value, (int)$decimal_place, $decimal_point, $thousand_point));
}

echo superscript_value(123456.789, '$') . "\n";
// $123,456<sup>.79</sup>
echo superscript_value(10.00, '$') . "\n";
// $10

$123456 0,79

$10

-1

HTML:

$i = 42;
$string .= "<sup>".$i."</sup>";

- 42.

-1

All Articles