Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

check if $val is a whole?

Comments in 'Plugin Development' started by dxm_hippie, Oct 10, 2015.

  1. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    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/$dc2);
  2. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    is $kc and $dc the kills and deaths?
  3. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    yes kill count and death count
  4. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    PEMapModder likes this.
  5. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    I was actually trying to add useless decimals :p 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..
    Falk likes this.
  6. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    But if +0 will take away the useless decimals..will -0 add one? 0.O doesn't seem to be right lol
  7. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    never mind i found a way :D
    PHP:
    $decimals = ( (int) $kdr != $kdr ) ? (strlen($kdr) - strpos($kdr'.')) - 0;
    PEMapModder and Falk like this.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I was thinking about this:
    PHP:
    $kdr round($kc $dc 100) / 100;
    :D
    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 :p
    dxm_hippie likes this.
  9. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    I would do this:
    PHP:
    $kdr sprintf("%.2f"$kc/$dc);
  10. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Thanks for the idea btw :p 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
  11. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    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
    Last edited: Oct 13, 2015
  12. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    so
    PHP:
    var_dump("" . (round($kc $dc 100) / 100));'
    will add the usless .00?
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't hardcode the color codes...
  14. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    What's that mean? don't use § after already using a TextFormat?
  15. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Or just don't use them a lot?..causes lag?
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't use the § character. That may get changed in the future.
  17. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Ah crap. I use that a lot lol TextFormat going to be around still?
  18. Syntaxious
    Offline

    Syntaxious New Member

    Joined:
    Sep 15, 2015
    Posts:
    19
    Minecraft User:
    Syntaxious
    I agree wth aliuly. This is what I would've used.

    PHP:
    $kdr = sprintf("%.2f", $kc/$dc);
  19. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    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.
  20. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    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

Share This Page

Advertisement