So i wasn't quite sure how to word the title lol what i got is a kill death ratio type deal going on and i need a way to check if the number is a whole or not..or possibly count the number of decimals? So for example if they have a kdr of 2/1 it will show 2.00 PHP: $kdr = round($kc/$dc, 2);
My first idea would be rtrim($str, "0"), but after searching I think there is a better way. I found a StackOverflow question (http://stackoverflow.com/questions/14531679/remove-useless-zero-digits-from-decimals-in-php) which seems to be what you're after. The accepted answer suggests casting the number into a float by adding 0 to it. So that would be $str + 0 and supposedly that would remove the useless decimals.
I was actually trying to add useless decimals just for the look kinda and thats how most games work like cod, like if they have 20 kills and 1 deaths instead of $kdr = round($kc/$dc, 2); showing 20 it will show 20.00 idk how that would work with round tho..
never mind i found a way PHP: $decimals = ( (int) $kdr != $kdr ) ? (strlen($kdr) - strpos($kdr, '.')) - 1 : 0;
I was thinking about this: PHP: $kdr = round($kc / $dc * 100) / 100; After converting it into a string, these are the results on my local tests: Code: [email protected]~~ MINGW64 / $ php -r '$kc = 20; $dc = 10; var_dump("" . (round($kc / $dc * 100) / 100));' string(1) "2" [email protected]~~ MINGW64 / $ php -r '$kc = 25; $dc = 10; var_dump("" . (round($kc / $dc * 100) / 100));' string(3) "2.5" [email protected]~~ MINGW64 / $ php -r '$kc = 26; $dc = 17; var_dump("" . (round($kc / $dc * 100) / 100));' string(4) "1.53" I used `"" . ` instead of `(string)` since I think that will be the way how you use the number (concatenate it with a string). I suppose there is no techincal difference though. The round(num * 100) / 100 method was supposed to be something used in languages like JavaScript where Math.round() doesn't have a decimal place precision parameter, but I find that it is useful here too
Thanks for the idea btw your killRate plugin is cool but i just wanted something simple and seems how i already have something that deals with deaths and kills i figured id just add a kill and death tracker to that. I hope to have it work with factionspro as well to get the kdr of a whole faction. Also your coding style is wild..in a good way lol i love the way you did killRate XD i didnt bite your code btw lol idk how to use databases yet
Instead of making a new thread..can anyone tell me how i might go about getting the top kdr? should i put topkills and topdeaths in a function then make sure the array key is the same and if not..uhh. idk how to go about that. PHP: case "topkills"; if($this->kills != NULL){ $kills = array_values($this->kills->getAll()); $topkills = max($kills); $name = array_search($topkills, $this->kills->getAll()); $sender->sendMessage(TextFormat::BOLD.TextFormat::AQUA.(strtoupper($name))." §2has the top kills of ".TextFormat::RED.$topkills); }else{ $sender->sendMessage(TextFormat::BOLD.TextFormat::RED."No kills found on this server"); } break; case "topdeaths"; if($this->deaths != NULL){ $deaths = array_values($this->deaths->getAll()); $topdeaths = max($deaths); $name = array_search($topdeaths, $this->deaths->getAll()); $sender->sendMessage(TextFormat::BOLD.TextFormat::AQUA.(strtoupper($name))." §2has died the most at ".TextFormat::RED.$topkills.TextFormat::BOLD." §2deaths"); }else{ $sender->sendMessage(TextFormat::BOLD.TextFormat::RED."No deaths found on this server"); } break; so i must find the array key that has the most kills..and least deaths but that also needs to be the highest kdr /: am i over complicating this?..lol
will try it, i got it but the way i found above is kinda a weird way to go about it i guess..it just counts the decimal placed then i must check if its null or 1 so on and so forth whats the deal with "%.2f" ? ive never seen that.
PHP: sprintf("%.2f") It basically means that the situation will be treated as a float. As it has a "2", the number will have 2 decimal points. Example: 10.78