Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Remove zero before decimal point

    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

  • #2
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    PHP Code:
    $string preg_replace("/0\./i""."$string); 

  • Users who have thanked digitalfiz for this post:

    Railsplitter44 (03-15-2008)

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,132
    Thanks
    2
    Thanked 328 Times in 320 Posts
    Probably faster -
    PHP Code:
    $string ltrim($string,"0"); 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    Railsplitter44 (03-15-2008)

  • #4
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    Probably faster -
    PHP Code:
    $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?

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Posts
    231
    Thanks
    9
    Thanked 1 Time in 1 Post
    I believe only if its a zero.

  • Users who have thanked Erindesign for this post:

    Railsplitter44 (03-15-2008)

  • #6
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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 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

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Um... you are echoing the result from number_format(), then trimming the zeros. Do you realize this?

    Flip the lines.

  • Users who have thanked kbluhm for this post:

    Railsplitter44 (03-15-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •