Hi,
If I have the following
00030
00005
02830
00002
00200
How do I remove the 0's preceding 0's so after formatted it looks like
30
5
2830
2
200
?????
string formatting
Moderator: General Moderators
Re: string formatting
I thought of using ltrim but i dont think that works with characters. Is there a php function that will remove preceding 0's starting from the left ?
Re: string formatting
try the number_format() function.
Code: Select all
$number = "00300";
print number_format($number);Re: string formatting
ltrim() is built to strip characters...so it'll work with characters.PHP Manual wrote:ltrim — Strip whitespace (or other characters) from the beginning of a string
Both ltrim() and number_format() would work, though without doing any testing, I'd guess ltrim() would be a few nanoseconds faster, though number_format() is probably the better choice, as you are formatting numbers.
You could also just add zero to all the numbers & print the result. I believe casting them to integers should also do the trick.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.