Hello everyone! I use PHP and need to remove the integer from the scene. I have 699.0143 and would like to work with .0143 Please@helpmeOK.com ps: should also work on 8999.123
Try this <?php function removeInt($number) { if (!is_numeric($number)) { return false; } else { if (!strstr($number,'.')) { $number = number_format($number,1,'.', ''); } $number = preg_replace('/([0-9]){1,}([.]){1}([0-9]*)/','$2$3',$number); return $number; } } // usage $number = 699.0143; $number2 = 8999.123; $number3 = 833.235534; $number4 = 89; print removeInt($number).'<br>'; // output .0143 print removeInt($number2).'<br>'; // output .123 print removeInt($number3).'<br>'; // output .235534 print removeInt($number4).'<br>'; // output .0 ?> PHP:
It took me a while to figure it out, but I got it to work and its just what the doctor ordered. Thanks you very much.
lol thats a big function just to do this: str_replace(intval($number),'',$number); Like einstein said: you write 100 pages of formulas just to say what i say in 6 characters
Unfortunately this suffers both a rounding bug & a potential collision if the int portions sequence is present in the float portion. I thought about using substr and fmod for a one-liner too, but it too suffers from a rounding bug.
Considering a variable is before all a string, you can also simply do: substr($number, strpos($number, "."), strlen($number)-strpos($number, ".")); PHP: Of course, this only works if the decimal point is present. Note that using regular expressions turns you code quite not readable. Always think about the programmers that comes after you.
Wow. Talk about overthought answers. Converting to strings, using explode, regexp parsing... for a bloody floating point operation?!? Mein gott.... and people wonder why I say the majority of php programmers couldn't code their way out of a piss soaked paper bag with a hole in the bottom. That every single answer so far is treating it as a string is just mind-blowing to me given how STUPID that is since we're talking about a NUMBER. (and numeric processing is generally multiple orders of magnitude faster than string operations) function removeInt($inFloat) { return $inFloat-floor($inFloat); } Code (markup): It's a float. Subtract the floor from the original. Basic ****** math people...
Converting to string? What are you talking about? In Php variables ARE strings!!! Well, we could have done this your way, it was simpler. But he said he wanted, for example, .178 from 69.178 and not 0.178, and that is what your function returns. Slight difference. Read the question before getting upset.
Is there any good tuto to learn how to use preg_replace, regular expressions are so powerful, but difficult .... thanks!
Floats in particular. As BP implied you CAN use typecasting and php DOES have types. It just uses LOOSE typecasting converting as needed on the fly between types. Because if all variables were just strings, and we set $a to 3, the operation $a+=1 should either cause a syntax error or return 31. One of the whole REASONS php uses '.' instead of '+' on strings is so it can quickly figure out you want a string operation without explictly converting it to string. Use a mathematical operator, you get the appropriate mathematical type - be it integer or float... and computers handle mathematical operations dozens if not hundreds of times faster than they can handle strings. Strings are slow - abysmally and painfully slow. (see why XML as a data storage medium is made of /FAIL/)
Hmm...exists a probability (extremely low but not zero) that the number vanishes from db and goes to mars, i think we should take that in count and parse it properly Basically everything you need is what i proposed as long you validade the number from user input, the rest is just quantic phisics that you dont need to understand to be able to use them...
It's called attention to detail, you should try it some time. The OPs example output didn't include a leading zero (0.1234), floating point operations are going to return a value with that leading zero. Programmers are only as good as their input.
His English is good enough to be understood by the most. Why not making an effort? Your code is as good as anyone's else and comply with what is expected. In fact the only code presented here that doesn't is deathshadow's, that criticizes everybody's answer. Very arrogant and rude. Certainly not the proper way to behave in a forum. His code would return 0.123 over 15.123. The expected answer should have been .123. Read the thread before giving an answer! It's really not the same (except for a calculator maybe). The best code presented here is certainly ads2help's. It gives the correct result, deals with exceptions, it's readable and comply with most inputs.
eekks... could say that was long... the first code..How about this <?php $str = "234.232"; $pieces = split(".",$str); $newstr = ".".$pieces[count($str) - 1]; // should give you .232 ?> Hope this makes it easy . Thanks imphpguru ?>