Can I use the $string number_format line to superscript my decimals?

杀马特。学长 韩版系。学妹 提交于 2019-12-22 11:04:02

问题


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

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

I'm not very smart but I have been trying since yesterday using different methods I've found searching stack overflow. Stuff like ."<sup> or just "<sup>" and also .'<sup>' and so many other combinations, but nothing works. I either get an error or the price disappears due to the error I introduce into the code because as I said I'm not the sharpest tool in the shed.


回答1:


Please check out the code I have below, this will format your string, and then use regular expressions to superscript the decimal

<?
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

$123,456.79

$10




回答2:


You should use HTML formatting for that:

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

It will produce something like 42.



来源:https://stackoverflow.com/questions/10462902/can-i-use-the-string-number-format-line-to-superscript-my-decimals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!