seany123 Posted April 19, 2009 Share Posted April 19, 2009 basically i have this code: <?=($player->exp / $player->maxexp * 100);?>% but it displays things like 50.5458565999% where i only want it to display 50% or 51% Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/ Share on other sites More sharing options...
alphanumetrix Posted April 19, 2009 Share Posted April 19, 2009 Well I couldn't write what I am thinking in that same syntax short-cut format, but essentially, you could try using a condition to round the results. example: $roundup = 235; $rounddown = 234; $t = 234.232; if ($t{5} >= 5) { echo $roundup; } else { echo $rounddown; } there's probably an easier way to do it, but you could get creative with that. Given what I see from your code, you're using classes. So perhaps create a method to sort it in that way, and instead of calling the variable directly like you did, call the value of a method. IE: <?=($player->expRounded() / $player->maxexp * 100;?> EDIT: here's an example of a method: <?php function expRounded() { $exp = $this->exp; $nodecimal = strstr($exp, '.', true); $last = sub_str($nodecimal, -1); $roundup = substr_replace($nodecimal, $last+1, -1, 0); $rounddown = substr_replace($nodecimal, $last-1, -1, 0); if ($last >= 5) { echo $roundup; } else { echo $rounddown; } } ?> I didn't test any of this, and don't plan to, but that should give you what you're looking for, for any number. Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814091 Share on other sites More sharing options...
seany123 Posted April 19, 2009 Author Share Posted April 19, 2009 im confused sorry where all these numbers are coming from etc. isnt there a way to use format_number...or anything like that? Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814139 Share on other sites More sharing options...
mrMarcus Posted April 19, 2009 Share Posted April 19, 2009 try number_format(), where $num is the number (percentage, etc.), that you would like rounding (formatting) : $num = 50.5458565999; echo number_format($num, 0, '.', ''); //returns 51 1 will round to the tenth .. 2 would round to the hundredth, 0 returns no decimal place(s), etc. the above will return 51. perhaps it will help. Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814141 Share on other sites More sharing options...
seany123 Posted April 20, 2009 Author Share Posted April 20, 2009 for that i will have to make a seperate variable and echo it.. would this work? $exppercent = ($player->exp / $player->maxexp * 100); echo number_format($exppercent, 0, '.', ''); Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814147 Share on other sites More sharing options...
teynon Posted April 20, 2009 Share Posted April 20, 2009 <?=round($player->exp / $player->maxexp * 100);?> Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814151 Share on other sites More sharing options...
mrMarcus Posted April 20, 2009 Share Posted April 20, 2009 try that : $exppercent = number_format(($player->exp / $player->maxexp * 100), 0, '.', ''); Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814152 Share on other sites More sharing options...
teynon Posted April 20, 2009 Share Posted April 20, 2009 PHP has a cool feature called round. Like i posted above mrMarcus. Use round to round. Use ceil to round up, use floor to round down. Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814154 Share on other sites More sharing options...
seany123 Posted April 20, 2009 Author Share Posted April 20, 2009 try that : $exppercent = number_format(($player->exp / $player->maxexp * 100), 0, '.', ''); thats exactly what i need. thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814157 Share on other sites More sharing options...
teynon Posted April 20, 2009 Share Posted April 20, 2009 Wait, why would you use that instead of round? What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814160 Share on other sites More sharing options...
seany123 Posted April 20, 2009 Author Share Posted April 20, 2009 Wait, why would you use that instead of round? What am I missing? nothing, its just i like to have my code a certain way so i understand it better... even though the round function sounds perfectly simple, ive never used it before... on other notes.. my entire site uses this method so making an exception just for this page doesnt sound right to me (id rather have it all consistant). thanks Quote Link to comment https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-815027 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.