如何分隔数字并获取PHP的前两位数字? [英] How can I separate a number and get the first two digits in PHP?

查看:311
本文介绍了如何分隔数字并获取PHP的前两位数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何分隔数字并获取PHP的前两位数字?

How can I separate a number and get the first two digits in PHP?

例如:1345->我想要此输出=> 131542我想要15.

For example: 1345 -> I want this output=> 13 or 1542 I want 15.

推荐答案

一种可能性是使用 substr :

echo substr($mynumber, 0, 2);


请不要那样做,就像 hakre 所说的那样,对于负数或带有小数位的小数,这会中断.他的解决方案是更好的解决方案,因为他正在做一些检查来避免这种情况.


please not that, like hakre said, this will break for negative numbers or small numbers with decimal places. his solution is the better one, as he's doing some checks to avoid this.

这篇关于如何分隔数字并获取PHP的前两位数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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