我可以使用$ string number_format行上标小数吗? [英] Can I use the $string number_format line to superscript my decimals?
问题描述
我想使以下字符串中的小数显示为上标:
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);
我不是很聪明,但是从昨天开始我一直在尝试使用发现的不同方法进行搜索堆栈溢出。像。< sup>
之类的东西,或者只是< sup>
和 。'< sup>'
以及许多其他组合,但无济于事。我要么得到一个错误,要么由于我在代码中引入的错误而使价格消失,因为正如我所说,我不是
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.
推荐答案
请查看下面的代码,这将格式化您的字符串,然后使用正则表达式来为小数加上上标
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
$123,456.79
$ 10
这篇关于我可以使用$ string number_format行上标小数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!