aljohnstone Posted February 18, 2011 Share Posted February 18, 2011 This works echo $data3['value']; But this doesn't echo round($data3['value'], 2); Can someone please advise me as to what I am doing wrong? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 18, 2011 Share Posted February 18, 2011 What do you mean by "this doesn't"? Ken Quote Link to comment Share on other sites More sharing options...
aljohnstone Posted February 18, 2011 Author Share Posted February 18, 2011 echo $data3['value']; returns: 1,384.1000 echo round($data3['value'], 2); returns: 1 I need it to round off the last two decimal places: 1,384.10 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 You need to remove the comma. It can't be type-juggled into a numeric data type with the comma in it. Quote Link to comment Share on other sites More sharing options...
aljohnstone Posted February 18, 2011 Author Share Posted February 18, 2011 The number which is dynamically generated has changed to 1,384.1500 If I take out the comma it returns 1,384.2500 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 Although I'm not sure what you're getting at in your last post, round simply isn't going to work on it at all if there's a comma in it. Quote Link to comment Share on other sites More sharing options...
aljohnstone Posted February 18, 2011 Author Share Posted February 18, 2011 My understanding is that in round() the comma seperates the parameters. The first parameter is the number that is being rounded which in this case is the value of the variable $data3['value'] and the second parameter is to how many places after the decimal which is in this case 2. If I remove the comma it does not limit the number of decimal places it just messes up the number that is returned. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 No, no, no . . . 1,384.2500 ^ That comma Quote Link to comment Share on other sites More sharing options...
cgsmith105 Posted February 18, 2011 Share Posted February 18, 2011 What Pikachu is saying is in the PHP manual which he linked to. Note: PHP doesn't handle strings like "12,300.2" correctly by default. See converting from strings. Quote Link to comment Share on other sites More sharing options...
lalnfl Posted February 18, 2011 Share Posted February 18, 2011 You could just do this, it would save you all the hassle: $value = $data3['value']; $value = round($value, 2); echo "$value"; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 How do you figure that would help? The value in that array element has a comma in it, so round() still won't work on it. Quote Link to comment Share on other sites More sharing options...
aljohnstone Posted February 18, 2011 Author Share Posted February 18, 2011 Oh OK. Sorry for being so thick. Can I strip out the comma and then apply round()? Quote Link to comment Share on other sites More sharing options...
aljohnstone Posted February 18, 2011 Author Share Posted February 18, 2011 I got it!!! $data4 = str_replace(",", "", $data3['value']); echo round($data4, 2); Thanks for your help! Quote Link to comment 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.