Reply
strip chars after period (200.000 => 200)
Old 05-09-2007, 04:50 PM strip chars after period (200.000 => 200)
Average Talker

Posts: 23
SOLVED: See next post.

I am trying to strip all the chars after the period. the value will range from being 3-4 chars, so i cant simply limit the string length. is there a function that will help me do this?

Last edited by RRasco : 05-09-2007 at 05:53 PM. Reason: solved
RRasco is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 05-09-2007, 05:53 PM Re: strip chars after period (200.000 => 200)
Average Talker

Posts: 23
SOLVED:

Code:
// Remove chars after . (200.000 => 200)
function removeDotZero($var)
{
    $var = trim($var);

    $pos = strpos($var,".");
    if($pos != false)
    {
        $size = strlen($var);

        for($i = $size; $i > $pos; $i--)
        {
            if(substr($var,-1,1) == "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")
                $var = substr($var,0,($i - 1));
            else if(substr($var,-1,1) == ".")
            {
                $var = substr($var,0,($i - 1));
                break;
            }
            else
                break;
        }

        return $var;
    }
    else
        return $var;
}
RRasco is offline
Reply With Quote
View Public Profile
 
Old 05-09-2007, 05:55 PM Re: strip chars after period (200.000 => 200)
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
Hmmm, why not use a native php function ?
http://www.php.net/manual/en/function.number-format.php

Or if you want to do it your way:
PHP Code:
function removeDotZero($var){
  
$ret=null;
  
$dotPos=strrpos(".",$var);  //Catch the last dot
  
if($dotPos!==FALSE){  // !== and === test the type AND the value, here that the return is false, and not 0
    
$ret=substr($var,0,-$dotPos);  //Cut your string until the position of the last dot
  
}
  return 
$ret;

__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 05-09-2007 at 05:59 PM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 05-09-2007, 05:56 PM Re: strip chars after period (200.000 => 200)
Average Talker

Posts: 23
Quote:
Originally Posted by tripy View Post
Hmmm, why not use a native php function ?
http://www.php.net/manual/en/function.number-format.php
haha, posting at the same time.

i might look into that, but i have it working for now. thanks!
RRasco is offline
Reply With Quote
View Public Profile
 
Old 05-09-2007, 05:59 PM Re: strip chars after period (200.000 => 200)
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
Quote:
haha, posting at the same time.
Posting and editing at the same time
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-09-2007, 06:00 PM Re: strip chars after period (200.000 => 200)
Average Talker

Posts: 23
actually, i just used that function, and its doing exactly what i needed it to...1 step further than my other listed function, thanks!
RRasco is offline
Reply With Quote
View Public Profile
 
Old 05-09-2007, 06:41 PM Re: strip chars after period (200.000 => 200)
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
If you're only working with numbers, you could also use the floor() function.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to strip chars after period (200.000 => 200)
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14772 seconds with 12 queries