问题
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