Jump to content

[SOLVED] help please.


seany123

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814091
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-814141
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/154797-solved-help-please/#findComment-815027
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.