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? Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/ 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 Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176009 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 Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176012 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. Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176024 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 Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176029 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. Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176034 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. Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176057 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 Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176060 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. Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176061 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"; Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176073 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. Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176076 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()? Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176078 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! Link to comment https://forums.phpfreaks.com/topic/228060-round-syntax-help/#findComment-1176084 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.