我可以使用$string number_format行来上标我的小数吗?


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

我想使以下字符串中的小数显示为上标:

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

不是很聪明,但从昨天开始我一直在尝试使用我发现搜索堆栈溢出的不同方法。像."<sup>或只是"<sup>",还有.'<sup>'和许多其他组合,但没有任何效果。我要么收到错误,要么由于我在代码中引入的错误而价格消失,因为正如我所说,我不是棚子里最锋利的工具。

请查看我在下面的代码,这将格式化您的字符串,然后使用正则表达式上标小数

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

您应该为此使用 HTML 格式:

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

它将产生类似 42 的东西。