Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/148727-solved-removing-commas-from-numbers/
Share on other sites

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.

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.

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

 

 

 

<?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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.