Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

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

like image 222
Jennifer Anthony Avatar asked Sep 14 '11 08:09

Jennifer Anthony


1 Answers

one possibility would be to use substr:

echo substr($mynumber, 0, 2);

EDIT:
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.

like image 175
oezi Avatar answered Sep 29 '22 12:09

oezi