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

Why doesn't round() work

Comments in 'Plugin Development' started by CraftYourBukkit, Sep 30, 2015.

  1. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Alright, so I am getting the distance between a player and all other players in a world, then out of that, get the nearest player. But the number is something like 2.325325234 even tho I want it to be 2. I tried round, but it didn't work(It was still that long number):
    PHP:
    $dis 500;
                
    $target null;
                foreach (
    $this->getServer()->getLevelByName("Wonderland")->getPlayers() as $players) {
                    
    $distance $player->getLocation()->distance($players->getLocation());
                    if (
    $players !== $player) {
                        if (
    $distance $dis) {
                            
    $dis $distance;
                            
    round($dis0); // This doesn't work
                            
    $target $players;
                        }
                    }
                }

                if (
    $target === null) {
                    
    $player->sendMessage("§7There's no nearest player.");
                } else {
                    
    $config = new Config($this->getDataFolder() . strtolower($target->getName()) . ".yml"Config::YAML);
                    
    $kit $config->getNested("kit");
                    
    $player->sendMessage("§7Nearest Player: §6" $target->getName() . "($dis)");
                    
    $player->sendMessage("§6" $target->getName() . "§7's Kit: §e" $kit);
                }
    Last edited: Sep 30, 2015
    PianoPlayer38 likes this.
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    What's $player?
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    $event->getPlayer(); at PlayerInteractEvent with a Compass
    PianoPlayer38 likes this.
  4. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    try just
    PHP:
     round($dis)
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    That'd be the same, and I already tried it.
    PianoPlayer38 likes this.
  6. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    PHP:
    $dis round($dis);
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    I'll try that out tomorrow, thanks:)
    PianoPlayer38 likes this.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    round() returns a rounded value of the input, not modify the input.
    Legoboy0215 and Lambo like this.
  9. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    I see that, thanks^^
    PianoPlayer38 and Legoboy0215 like this.
  10. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Why not use
    floor($dis);
  11. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    If the number is 14.5914, I want it to be rounded, so It'd be 15. With floor() it'd be 14
  12. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Last edited: Oct 2, 2015
  13. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PianoPlayer38 and PEMapModder like this.
  14. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Yes
    PianoPlayer38 likes this.
  15. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Note that unless your integer is exceptionally large (larger than PHP_INT_MAX, i.e. 2^31 - 1 for 32-bit systems and 2^63 - 1 for 64-bit systems), it is faster to use (int) to floor the number. This difference is notable for repeated operations in PHP 5.
  17. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Did he ask about flooring numbers? I don't think so. He was asking why round() didn't work, and round() doesn't directly round a number down, it can round it up also. Please read the first post next time. Did you just look at the last post made before you posted?
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I obviously did. Read my posts before. I already addressed the issue in the first post, and we are now talking about something else. Did any rules tell you that there must not be posts replying to questions that emerge from the original issue? I am writing that to avoid confusion in the future on the issues that emerged from posts in this issue. If we ignore it, a likely result is that the wrong thing will last and magnify in the future.

Share This Page

Advertisement