Question:
How to remove leading zero from a number in PHP?
?
2008-09-03 10:27:31 UTC
I have this TEXT FIELD which suppose to accept only numbers. It's actually a field for "price". I manage to find a javascript for accepting only numbers. Now I'm having issue when user keyed in 00030 into the field. When it's being save into the database and displayed out to the website, it's displays as $00030. How do I remove the 3 zeros or any zeros before it?
Three answers:
CodePHP.co.uk
2008-09-05 09:49:55 UTC
You can use one of two functions:

number_format($number);

or

money_format($number);



This will remove all the prefix 0's and format it correctly as a number. I have linked the the php.net manual
?
2016-11-16 20:43:36 UTC
Php Leading Zeros
Sleeping Troll
2008-09-03 10:38:22 UTC
If you add a string to a number in php it will convert string to number, default format for number is hide leading zeroes, so '00030' + 0 should result in 30.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...