如何删除PHP中小数点后第三位后的结尾零? [英] How to delete trailing zeros after the 3rd decimal place in PHP?

查看:200
本文介绍了如何删除PHP中小数点后第三位后的结尾零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预先感谢。



我有一家WP WooCommerce商店,需要上传一些具有小数点后三位的价格,例如£0.012(购买数量为000的产品)。



我的大部分产品都是普通产品,保留小数点后两位。



WooCommerce中有一个函数允许保留小数点后3位-很好。也是删除尾随零的函数,但是如果是整数,则会删除尾零。 £10.00变成£10。



当普通价格产品的95%开始显示£10,000或£5.230时,我的问题就出现了。


简而言之,我正在寻找一种删除尾随零但仅在小数点后三位之后的方法;



保留-£0.012
删除价格为£10.00或£5.23的第3个小数0



有人能找到好的解决方案吗?



谢谢

解决方案

如果要使用正则表达式,可以将它们与


$ b匹配$ b

 (?< = \d {2})(0 +)$ 

preg_replace( /(?< = \ d {2})(0 +)$ /,,$ price_string)

至少两位数字之后的所有零。 (它将与括号内的零匹配):

  12.002(0)
12.00(0000)
12.01(000000)
12.232(0)
12.123


thanks in advance.

I have a WP WooCommerce store and need to upload some prices that have 3 decimal places, e.g. £0.012 (products that are purchased in volumes of 000s).

The majority of my products are 'ordinary' with 2 decimal places.

There is a function in WooCommerce that allows for 3 decimal places - fine. Also a function to delete trailing zeros, but it deletes them if it's an integer e.g. £10.00 becomes £10.

My problem arises when the 95% of 'ordinary' price products start showing £10.000 or £5.230.

In a nutshell I'm looking for a way to delete trailing zeros but ONLY after the 3 decimal place;

Retain - £0.012 Delete any 3rd decimal 0 on prices like £10.00 or £5.23

Does anyone have a good solution?

Thanks

解决方案

If you want to use regular expressions you can match them with

 (?<=\d{2})(0+)$

 preg_replace("/(?<=\d{2})(0+)$/", "", $price_string)

to match all zeroes which come after at least two digits. (It will match the zeroes in parenthesis):

12.002(0)
12.00(0000)
12.01(000000)
12.232(0)
12.123

这篇关于如何删除PHP中小数点后第三位后的结尾零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆