从sql查询格式化十进制输出(Format decimal output from sql query)

是否有可能以html方式将以下sql查询的结果拆分为样式小数?

<?php $sql = "SELECT * FROM vendordb_3_postmeta WHERE meta_id = 25"; foreach ($pdo->query($sql) as $row) { echo $row['meta_value']."<br />"; } ?>

输出是产品价格,如999.99。目的是设置小数上标的样式。

Is it possible to split up the result of the following sql query in html that way to style the decimals?

<?php $sql = "SELECT * FROM vendordb_3_postmeta WHERE meta_id = 25"; foreach ($pdo->query($sql) as $row) { echo $row['meta_value']."<br />"; } ?>

The output is a product-price like 999.99 The aim is to style the decimals superscript.

最满意答案

如果要格式化字符串的一部分,则需要将其拆分为可单独格式化的片段。

如果你想要一个像这样的结果

999 99

你可以使用explode()将其拆分。 这会将您的999.99拆分为一个包含两个元素999和99的数组(之前的内容和分隔符之后的内容)。 然后你可以像下面那样格式化它

$values = explode(".", $row['meta_value']); echo $values[0]."<sup>".$values[1]."</sup><br />";

参考

http://www.php.net/explode

If you want to format a part of the string, you'll need to split it up into pieces that can be formatted separately.

If you want a result like

99999

you can split it up using explode(). This will split your 999.99 into an array with two elements, 999 and 99 (what's before and what's after the separator). You can then format it like below

$values = explode(".", $row['meta_value']); echo $values[0]."<sup>".$values[1]."</sup><br />";

References

http://www.php.net/explode

更多推荐