computermax2328 Posted August 19, 2012 Share Posted August 19, 2012 Hello Again, I have a data base that is full of averages, for example (.124). They are stored in the database like this (0.124). Of course without the parentheses on each example. I am using an equation to get these numbers and just for the record the equation is not the problem. To get rid of the leading zero in (0.124) I am using ltrim. For example: $num = 0.124; ltrim($num, 0); Now my equation is giving me negative numbers, which is fine. The problem is that the ltrim command is not working on the negative numbers. The number I am getting when I output is (-0.124). I want the number to be (-.124). Any suggestions?? Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/ Share on other sites More sharing options...
jackr1909 Posted August 19, 2012 Share Posted August 19, 2012 Hi, first of all, using ltrim with the value of 0 might lead to problems with significant figures (remember, after the decimal point, every 0 is as important as all the other numbers, because it holds a place. Just as 0.3045 != 0.345 Here's what i would do: Find the negative numbers (preg_match maybe) and multiply them by -1 and then trim the 0 and multiply by negative one again Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370595 Share on other sites More sharing options...
jazzman1 Posted August 19, 2012 Share Posted August 19, 2012 I don't know,it's the best way to solve this issue, but I found one few months ago. Check out in php.net, there are lots of examples... $num = 0.123; print intval(strval($num*1000)); // prints 123 Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370597 Share on other sites More sharing options...
Christian F. Posted August 19, 2012 Share Posted August 19, 2012 I'm afraid both of you are wrong. ltrim () will not alter the 0 in the middle of a number, contrary to what jackr1909 stated. Yours, jazzman1, won't do what the OP wants to do. The correct solution would be to use preg_match (), like jackr1909 was alluding to. Though, not quite in the same way as he thought: if (preg_match ('/(\\-?)0(\\.\\d+)/', $num, $matches)) { $num = $matches[1].$matches[2]; } Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370600 Share on other sites More sharing options...
jazzman1 Posted August 19, 2012 Share Posted August 19, 2012 I want the number to be (-.124). I'm thinking, that could be easily done, but you need to put a dot between it. Create a new variable as string and add it a dot symbol. I don't have too much ideas about it // negative number $num = 0.124; print intval('-'.strval($num*1000)); // prints -124 Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370601 Share on other sites More sharing options...
jazzman1 Posted August 19, 2012 Share Posted August 19, 2012 @Christian, the problem is that's invalide integer. See your example: $num = 0.123; if (preg_match ('/(\\-?)0(\\.\\d+)/', $num, $matches)) { $num = $matches[1].$matches[2]; } var_dump(intval('-'.$num)); Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370602 Share on other sites More sharing options...
Pikachu2000 Posted August 19, 2012 Share Posted August 19, 2012 The number is a decimal. What does intval() have to do with it? Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370603 Share on other sites More sharing options...
Christian F. Posted August 19, 2012 Share Posted August 19, 2012 Jazzman1: I think you should read the OP again, particularly this bit: The problem is that the ltrim command is not working on the negative numbers. As for whether or not my code works: php > $num = -0.123; php > if (preg_match ('/(\\-?)0(\\.\\d+)/', $num, $matches)) { php { var_dump ($matches); php { $num2 = $matches[1].$matches[2]; php { } array(3) { [0]=> string(6) "-0.123" [1]=> string(1) "-" [2]=> string(4) ".123" } php > var_dump ($num2); string(5) "-.123" php > $num = 0.456; php > if (preg_match ('/(\\-?)0(\\.\\d+)/', $num, $matches)) { php { var_dump ($matches); php { $num2 = $matches[1].$matches[2]; php { } array(3) { [0]=> string(5) "0.456" [1]=> string(0) "" [2]=> string(4) ".456" } php > var_dump ($num2); string(4) ".456" Yes, I know it's an invalid float (integers are whole numbers), but that's what the OP wants. Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370605 Share on other sites More sharing options...
jazzman1 Posted August 19, 2012 Share Posted August 19, 2012 I don't know who is wrong and who is not. I just said that this is invalid number - -.124 If the OP wants to return a string from number, why should defined $num = 0.124 as decimal, does not make any sense for me, right? Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370606 Share on other sites More sharing options...
Christian F. Posted August 19, 2012 Share Posted August 19, 2012 I don't know why he'd want that either, but I guess it's purely for output formatting. *Shrugs* Trick isn't as much as making sense of why the poster would want to do something, at certain times, but what s/he's trying to do in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370607 Share on other sites More sharing options...
Pikachu2000 Posted August 19, 2012 Share Posted August 19, 2012 I don't know who is wrong and who is not. I just said that this is invalid number - -.124 If the OP wants to return a string from number, why should defined $num = 0.124 as decimal, does not make any sense for me, right? Why is -.124 an invalid number? Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370611 Share on other sites More sharing options...
Christian F. Posted August 19, 2012 Share Posted August 19, 2012 Just tested: It isn't. Never knew that, so thanks for the heads up, Pika. Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370612 Share on other sites More sharing options...
jazzman1 Posted August 19, 2012 Share Posted August 19, 2012 Maybe in China is a valid number Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370614 Share on other sites More sharing options...
Pikachu2000 Posted August 19, 2012 Share Posted August 19, 2012 Just what, exactly, makes you think it isn't a valid number? Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370616 Share on other sites More sharing options...
jazzman1 Posted August 19, 2012 Share Posted August 19, 2012 Hm.....you are right $num = -.123; var_dump($num); Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370617 Share on other sites More sharing options...
jazzman1 Posted August 19, 2012 Share Posted August 19, 2012 Ok, the question is, now to convert 0.123 to number -.123? PS, Pika good spotting my friend Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370618 Share on other sites More sharing options...
Christian F. Posted August 19, 2012 Share Posted August 19, 2012 No, the question was to change -0.123 to -.123. The OP stated that his code worked for positive numbers, but not negative. Which is what he needed help with. Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370632 Share on other sites More sharing options...
Psycho Posted August 19, 2012 Share Posted August 19, 2012 There's a very simple solution: str_replace() Since the values are always decimals less than 1 and greater than -1, you can simply replace '0.' with '.' echo str_replace('0.', '.', '0.123'); // .123 echo str_replace('0.', '.', '-0.123'); // -.123 Of course this would not work if there were numbers such as 10.123 Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370636 Share on other sites More sharing options...
computermax2328 Posted August 19, 2012 Author Share Posted August 19, 2012 I went with the str_replace(). That was the most effective method. Thanks again, Quote Link to comment https://forums.phpfreaks.com/topic/267295-ltrim-negative-numbers/#findComment-1370692 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.