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. Offline

### dxm_hippieActive MemberPlugin Developer

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

### Hotshot_9930Notable MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### FalkStaff MemberGlobal Moderator

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

### dxm_hippieActive MemberPlugin Developer

Joined:
Feb 1, 2015
Posts:
413
Plugins:
1
Minecraft User:
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..
Falk likes this.
6. Offline

### dxm_hippieActive MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### PEMapModderNotable MemberPlugin Developer

Joined:
Oct 9, 2013
Posts:
7,294
Plugins:
11
Minecraft User:
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
dxm_hippie likes this.
9. Offline

### aliulyNotable MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### PEMapModderNotable MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### PEMapModderNotable MemberPlugin Developer

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

### dxm_hippieActive MemberPlugin Developer

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

### SyntaxiousNew Member

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

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

### dxm_hippieActive MemberPlugin Developer

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

### zombie_PowerActive Member

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

Advertisement