DolDurma
DolDurma

Reputation: 17311

PHP remove last zero from integer by substr

in php i get some values from database as integer, now i want to remove last zero from 20000000 integer, but me below code doesnt work.

$payerd_amount = 20000000;
$payerd_amount = substr((string)$payerd_amount,1,strlen((string)$payerd_amount) -1);

how to do this action? my code seems correct but after substring i get this result:

000

Upvotes: 0

Views: 1474

Answers (4)

Andreas
Andreas

Reputation: 23958

If it does not work on all integers to just devide by 10, it probably is because of rounding?
So...

$newint = floor($payerd_amount / 10);
// if the number is 123456, newint will be 12345

Upvotes: 3

Jakir Hossain
Jakir Hossain

Reputation: 2517

You can start from 0 replace with 1. Example

$payerd_amount = 20000000;
$payerd_amount = substr((string)$payerd_amount,0,strlen((string)$payerd_amount) -1);
print($payerd_amount);

Output

2000000

Upvotes: 1

Rid Iculous
Rid Iculous

Reputation: 3962

If all you want to do is remove the last zero, just divide by 10

   $payerd_amount = $payerd_amount / 10;

This works, too:

   $payerd_amount = substr($payerd_amount, 0, -1);

Upvotes: 2

Waqas Shahid
Waqas Shahid

Reputation: 1060

Try this:

$payerd_amount = substr($payerd_amount, 0, -1);

Manual

Upvotes: 0

Related Questions