lovespring
lovespring

Reputation: 19589

How to get the first two numbers in an integral?

How to get the first two numbers in an integral?

For example: get 12 of the 123456 in PHP or JavaScript?

Upvotes: 9

Views: 7652

Answers (4)

Guffa
Guffa

Reputation: 700690

Numerically in Javascript:

while (number >= 100) number = Math.floor(number / 10);

Upvotes: 1

Delan Azabani
Delan Azabani

Reputation: 81472

JavaScript:

(123456).toString().substr(0,2);

PHP:

substr(123456, 0, 2);

Upvotes: 7

Sarfraz
Sarfraz

Reputation: 382841

PHP:

$num = 123456;
echo substr($num, 0, 2);

JavaScript:

alert((123456).toString().substr(0, 2));

Upvotes: 17

sea_1987
sea_1987

Reputation: 2954

$result = substr("123456", 0, 2);

Upvotes: 2

Related Questions