View Full Version : Remove zero before decimal point

03-14-2008, 02:08 AM
Hi, I'm new to coding but I'm definitely willing and trying to learn. I'm using dreamweaver and I've connected a sql database to my site. Many of my columns contain numbers between 0 and 1 like .357 and .582. Unfortunately though, it displays on my site as 0.357 and 0.582.

I'm looking for code that will remove the zero in front of the decimal point so that it won't display on the site. Can anyone help me out? it would be greatly appreciated. Thanks

03-14-2008, 02:31 AM
$string = preg_replace("/0\./i", ".", $string);

03-14-2008, 02:58 AM
Probably faster -

$string = ltrim($string,"0");

03-14-2008, 03:05 AM
Probably faster -

$string = ltrim($string,"0");

Does this remove the left digit ONLY if it is a zero? or does it remove the left digit no matter what?

03-14-2008, 04:25 AM
I believe only if its a zero.

03-14-2008, 04:33 AM
The code worked great, thank you all very much for your help. I've tried multiple ways to combine this ltrim function with the format_number function but I'm having no success. Here is my code:

<?php echo number_format($row_Batting1['BA'],3,'.',',');
$row_Batting1['BA'] = ltrim($row_Batting1['BA'],"0"); ?>

Unfortunately, it only processes the first function and ignores the second. Any ideas? Thanks

03-14-2008, 05:57 PM
Um... you are echoing the result from number_format(), then trimming the zeros. Do you realize this?

Flip the lines.