1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need PHP code to remove 699 from 699.0143

Discussion in 'PHP' started by please, Oct 13, 2008.

  1. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #21
    Because it was gibberish?

    I'm sorry, but bad code and overthought methodology is something I rail against because there is too much COMPLETE CRAP written in php - see turdpress. That and I spent most of last week cleaning up PHP so bad it makes the example in my 'good code, bad code' post look good.

    Of course, god forbid someone see crap and then call it such.

    Ooh, so substr. BFD. I made the assumption he needed the numeric value - since I can't see many cases of needing it as just the string.

    Funny I consider it the worst because it uses a convoluted regex, handles the least likely condition first, though it does as you point out at least have exception handling - the is_numeric check is something that should be present in all versions.

    Assuming you want it without the leading zero, and as such actually need exception handling, I'd approach that as this:
    function removeInt($number) {
    	if (is_numeric($number)) {
    		$number=abs($number);
    		$number-=floor($number);
    		return ($number!=0) ? substr($number,1) : '.0';
    	} else return false;
    }
    Code (markup):
    It also strips the sign.

    Feed it a number without a decimal. You end up with only one array element so your answer points at an undefined. Also your index is incorrect, you should be accessing count($pieces) not count($str).

    Any method/attempt to do this should probably be tested against this:
    echo '
    	Test 1 - removeInt(123.234) == ',removeInt(123.234),'<br />
    	Test 2 - removeInt(.234) == ',removeInt(.234),'<br />
    	Test 3 - removeInt(123) == ',removeInt(123),'<br />
    	Test 1 - removeInt(-123.234) == ',removeInt(-123.234),'<br />
    	Test 4 - removeInt(-.234) == ',removeInt(-.234),'<br />
    	Test 5 - removeInt(-123) == ',removeInt(-123),'<br />
    	Test 6 - removeInt(0) == ',removeInt(0),'<br />
    	Test 7 - removeInt(\'test\') == ',(removeInt('Test') ? 'True' : 'False'),'
    ';
    Code (markup):
    If you literally just want the decimal without case or any leading characters it should return:

    Test 1 - removeInt(123.234) == .234
    Test 2 - removeInt(.234) == .234
    Test 3 - removeInt(123) == .0
    Test 1 - removeInt(-123.234) == .234
    Test 4 - removeInt(-.234) == .234
    Test 5 - removeInt(-123) == .0
    Test 6 - removeInt(0) == .0
    Test 7 - removeInt('test') == False


    Which is what the version I put in this post does. Ads2help's version returns:

    Test 1 - removeInt(123.234) == .234
    Test 2 - removeInt(.234) == .234
    Test 3 - removeInt(123) == .0
    Test 1 - removeInt(-123.234) == -.234
    Test 4 - removeInt(-.234) == -.234
    Test 5 - removeInt(-123) == -.0
    Test 6 - removeInt(0) == .0
    Test 7 - removeInt('test') == False


    Which could probably be corrected by tweaking the regex, or you could simply abs the value if indeed all you want is the decimal part as a string without preserving case.

    The versions trying to use split to do it have problems too. If you index off the number of values returned in your array, you get (assuming you add the numeric check):

    Test 1 - removeInt(123.234) == .234
    Test 2 - removeInt(.234) == .234
    Test 3 - removeInt(123) == .123
    Test 1 - removeInt(-123.234) == .234
    Test 4 - removeInt(-.234) == .234
    Test 5 - removeInt(-123) == .-123
    Test 6 - removeInt(0) == .0
    Test 7 - removeInt('test') == False


    If you were to use the version that assumes the explode/split will have two results have problems too.

    Test 1 - removeInt(123.234) == .234
    Test 2 - removeInt(.234) == .234
    Test 3 - removeInt(123) == .
    Test 1 - removeInt(-123.234) == .234
    Test 4 - removeInt(-.234) == .234
    Test 5 - removeInt(-123) == .
    Test 6 - removeInt(0) == .

    Test 7 - removeInt('test') == False


    Of course if you need the fraction to perform numeric operations on it, most of the above are useless because they do not preserve case - this is where ads2help's version actually would work to a degree, but if preserving negative numbers are important, it is unlikely that the leading zero matters so there are faster ways of doing it.

    To do that as math you have the problem that floor returns the next LOWEST whole integer, so floor(-123.234) returns -124, so $number-floor($number) would return +0.766 (minus a minus). As such you would want to trap negative numbers and use ceil instead.

    function removeInt($number) {
    	if (is_numeric($number)) {
    		return ($number>=0) ? $number-floor($number) : $number-ceil($number);
    	} else return false;
    }
    
    Code (markup):
    Which returns this:

    Test 1 - removeInt(123.234) == 0.234
    Test 2 - removeInt(.234) == 0.234
    Test 3 - removeInt(123) == 0
    Test 1 - removeInt(-123.234) == -0.234
    Test 4 - removeInt(-.234) == -0.234
    Test 5 - removeInt(-123) == 0
    Test 6 - removeInt(0) == 0
    Test 7 - removeInt('test') == False

    No colour highlighting of wrong/right since our result needs are different, so what's output should be different.

    ...and if for some bizarre reason one REALLY wants negative numbers preserved without the leading zero as ads2help's version did, then you can take the above math version and just do some substr trickery upon it.

    function removeInt($number) {
    	if (is_numeric($number)) {
    		$retVal=($number>=0) ? $number-floor($number) : $number-ceil($number);
    		if ($retVal>0) {
    			return substr($retVal,1);
    		} else if ($retVal<0) {
    			return '-'.substr($retVal,2);
    		} else return '.0';
    	} else return false;
    }
    Code (markup):
    Which I'm not wild about because of the extra conditionals, but at least it doesn't need a regex and returns output identical to ads2help's.
     
    deathshadow, Oct 15, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #22
    i saw my name a lot times >.<
    anyway it depends on what the person want and how he want to use the number after removeInt()
    deathshadow's would be faster & better no doubt
     
    ads2help, Oct 15, 2008 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #23
    Sorry 'bout that, yours was a... creative solution, and certainly the closest of those presented despite the issues that IMHO make it a bad approach.

    Just occured to me Clades may be on the right track using IntVal - though why waste the function call if we could just typecast as (int)?

    So scratch my previous examples - replace them with these:

    For no sign preservation 'just everything after the decimal':
    function removeInt($number) {
    	if (is_numeric($number)) {
    		$number=abs($number);
    		$number-=(int)$number;
    		return ($number!=0) ? substr($number,1) : '.0';
    	} else return false;
    }
    Code (markup):
    For pure numeric:
    function removeInt($number) {
    	if (is_numeric($number)) {
    		return $number-((int)$number);
    	} else return false;
    }
    Code (markup):
    ... and for the oddball hybrid:
    function removeInt($number) {
    	if (is_numeric($number)) {
    		$retVal=$number-((int)$number);
    		if ($retVal>0) {
    			return substr($retVal,1);
    		} else if ($retVal<0) {
    			return '-'.substr($retVal,2);
    		} else return '.0';
    	} else return false;
    }
    Code (markup):
    Which would be even faster than calling floor or intval. Just typecast it to integer.

    There's always ways of making code leaner/faster - it's just a matter of sitting down and thinking about it.
     
    deathshadow, Oct 15, 2008 IP
  4. clades

    clades Peon

    Messages:
    579
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #24
    At least i'm not a chicken ;)
     
    clades, Oct 17, 2008 IP