Yammyguy Posted March 10, 2009 Share Posted March 10, 2009 I'm hoping someone can help me with a quick solution to this problem... Some of the numbers I'm processing are in the thousands, and therefore have commas. The code I have now is $val = str_replace(',','',$line_info[$i]); but for some reason this "str_replace()" isn't removing the commas from my number... what's being done wrong? thanks in advance for any help! Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/ Share on other sites More sharing options...
.josh Posted March 10, 2009 Share Posted March 10, 2009 There's nothing wrong with the code you provided. that code removes all commas from whatever is in element $i of $line_info and assigns the result to $val. Now if you want to know why it's not working within the context of your code, perhaps you should post the code it's being used in. Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-780935 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 works fine i think you haven't added the str_replace in the loop. <?php $line_info=array("1,",",2"); for($i=0; $i<count($line_info); $i++){ $val=str_replace(',','',$line_info[$i]); echo $val; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-780939 Share on other sites More sharing options...
Yammyguy Posted March 11, 2009 Author Share Posted March 11, 2009 In the array $line_info, I have numbers (790.897, 120.762, 87.6732,1243.9876). But any numbers in the thousands are displayed as "1,243.9876". This is because I imported these values via a text file. I'm trying to massage the numbers before re-outputting them. Things like only displaying 2 decimal places. I noticed that the numbers with commas will not properly format to two decimal places... So that's what I need to fix. Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-782297 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 can you post the whole chunk of relevant code ? Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-782308 Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 you can number format the number's with the following links. http://uk.php.net/sprintf http://uk.php.net/manual/en/function.number-format.php Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-782333 Share on other sites More sharing options...
Yammyguy Posted March 11, 2009 Author Share Posted March 11, 2009 This is really the only relevant code for this... I will also give you the actual values from a print_r() through the different formatting points that I've gone through, as well as what the values should be... print_r($data) before the str_replace, and number_format() is applied: array([0] => 734.5369 [1] => 250.2526 [2] => 0 [3] => 0 [4] => 1,007.5307 [5] => 1,026.2838 [6] => 0.8281 [7] => 0.7834) print_r($data) with number_format() applied: array_push($data, number_format($line_info[$i],2)); array([0] => 734.54 [1] => 250.25 [2] => 0.00 [3] => 0.00 [4] => 1.00 [5] => 1.00 [6] => 0.83 [7] => 0.78) Now you can see where the problem is. The number_format() function works great for returning only 2 decimal places, but with Keys 3, and 4 having a comma in the number they shouldn't be just 1, they should be 1007.53, and 1026.28 respectively, so my next step would be to remove the commas in those two values before running number_format(). So what I did was change the array contents to a $val after running the str_replace() function, then applying number_format(): $val = str_replace(',','',$line_info[$i]); array_push($data, number_format($val,2)); and here's the output. The rounding of the decimal places is no longer valid, and it's still returning a value of "1" for keys 3, and 4... array([0] => 734.00 [1] => 250.00 0.00,0.00,1.00,1.00,0.00 Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-782436 Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 is it all within the loop. Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-782439 Share on other sites More sharing options...
samshel Posted March 11, 2009 Share Posted March 11, 2009 <?php $arr = array(734.5369,250.2526, 0, 0, "1,007.5307", "1,026.2838", 0.8281, 0.7834); $cnt = count($arr); $newarr = array(); for($i=0;$i<$cnt;$i++) { $newarr[] = str_replace(",", "", $arr[$i]); } print_r($newarr); ?> this works for me... Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-782440 Share on other sites More sharing options...
Yammyguy Posted March 11, 2009 Author Share Posted March 11, 2009 <?php $arr = array(734.5369,250.2526, 0, 0, "1,007.5307", "1,026.2838", 0.8281, 0.7834); $cnt = count($arr); $newarr = array(); for($i=0;$i<$cnt;$i++) { $newarr[] = str_replace(",", "", $arr[$i]); } print_r($newarr); ?> this works for me... that's pretty much exactly what I have, but I can't put the two values in the array that are in the thousands in quotes (""). these values are drawn from a text file. Does this mean, I'm going to have to concatenate a " to the front and back of every value pulled from the text file? Quote Link to comment https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/#findComment-782448 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.