number with % (percent) sign change to number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

number with % (percent) sign change to number

Post by myplaces »

I have two forms, where user must enter number
First form - number (for example - 100)
Second form - percent. User must enter number without percent sign (for example- 10)

Then i do calculations.
First number multiply by percent (100*10/100)
But if user will enter not 10, but 10% , then calculation will be 100*10%/100

So i need to convert possible 10% to 10

may be it is simple solution, but my programming knowledge is very poor.....

hmmm, it seem it automaticaly is number? If I enter number with % sign, in calculation is used only number without percent sign?
Is it true?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: number with % (percent) sign change to number

Post by Eran »

PHP will automatically convert string into number when used with math operators. You can make sure by casting it into an integer -

Code: Select all

$number = (int) $number;
myplaces
Forum Newbie
Posts: 11
Joined: Wed Nov 12, 2008 3:25 pm

Re: number with % (percent) sign change to number

Post by myplaces »

Thanks a lot.
Post Reply