Forum Moderators: coopster

Message Too Old, No Replies

How to eliminate zeroes before the numbers

To eliminate the zeroes

         

Madhu

6:15 am on Jul 12, 2007 (gmt 0)

10+ Year Member



Hi,
I have to eliminate the zeros before the numbers .how can i do it .Any body plzzzzzzzz.

Madhu.

Habtom

6:34 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ltrim [php.net] can remove characters from the left side.

<?php
$text = "0002460";
$trimmed = ltrim($text, "0");
echo $trimmed; //outputs 2460
?>

Madhu

6:45 am on Jul 12, 2007 (gmt 0)

10+ Year Member



Thank u so much Habtom!

tomda

6:59 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



number_format() [us2.php.net] can also do the trick

Madhu

8:27 am on Jul 12, 2007 (gmt 0)

10+ Year Member



What wrong with this program?
It is not working.........

function trimfunc($number)
{
$trimmed = ltrim($number, "0");
return $trimmed; //outputs 460
}
echo "The trimmed value is".trimfunc(0045);
?>

Madhu

8:30 am on Jul 12, 2007 (gmt 0)

10+ Year Member



Plz help..........

Habtom

8:33 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function trimfunc($number)
{
$trimmed = ltrim($number, "0");
return $trimmed;
}
$input = "0045";
echo "The trimmed value is ". trimfunc($input);

tomda

8:36 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using number_format and not ltrim...

If I am not wrong, ltrim() is a string-function so it is not the best function to use since a number is a number and not a tring. number_format is a number-function.

I can't test your code today, my PC is down...

Habtom

8:39 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using number_format() as Tomda mentioned, ltrim is more of for strings and not for numbers as such.

Madhu

8:41 am on Jul 12, 2007 (gmt 0)

10+ Year Member



Very tanx for u both......
My code is now working..
I am new to php & to this forum
Tell me were is the log out link for this forum

tomda

8:46 am on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad it works!
To log out, go to your control panel and click on signout

Solution1

3:54 pm on Jul 15, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Even simpler: intval()

DrDoc

5:37 pm on Jul 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... or * 1

Habtom

4:53 am on Jul 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DrDoc :)

Yea, I don't see anything simpler than that.

$n = "000567";
echo $n;
$n = $n * 1;
echo "<br>";
echo $n;

// 000567
// 567

Achernar

5:00 pm on Jul 18, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



I tend to prefer: +0