Jump to content

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

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.