Raised This Month: $12 Target: $400
 3% 

Solved Mathematical Problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
THC420
Junior Member
Join Date: Apr 2018
Old 04-16-2018 , 10:45   Mathematical Problem
Reply With Quote #1

So I want to know what percentage of the level he is at, but the number displayed in the hud is always wrong
PHP Code:
        porcentaje = (g_ammopacks[id] / levelsAmmopack(g_level[id]))*100 


and this is how i display it
PHP Code:
         ShowSyncHudMsg(ID_SHOWHUDg_MsgSync2"|Vida: %s|^n|Chaleco: %s|^n|Level: %s|^n|AmmoPacks: %s/%s|^n|Restantes: %s (%d%%)|^n|Clase: %s|^n|Velocidad: %.0f|"AddComma(pev(ID_SHOWHUDpev_health)), AddComma(get_user_armor(id)), AddComma(g_level[id]), AddComma(g_ammopacks[ID_SHOWHUD]),AddComma(levelsAmmopack(g_level[id])), AddComma(restantes), porcentaje, class, spd

in the case of this picture it has to be 23.08 % the porcentage displayed, but it is 100 :s
Attached Thumbnails
Click image for larger version

Name:	zp.png
Views:	78
Size:	25.8 KB
ID:	169354  

Last edited by THC420; 04-18-2018 at 09:05.
THC420 is offline
E1_531G
Senior Member
Join Date: Dec 2017
Old 04-16-2018 , 14:39   Re: Mathematical Problem
Reply With Quote #2

Quote:
Restantes: %s (%d%%)
%d isn't for float. Use %f.
__________________
My English is A0
E1_531G is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 04-16-2018 , 21:12   Re: Mathematical Problem
Reply With Quote #3

Code:
Restantes: %s (%d%%)
Use %.2f%% instead of %d%%. When 2 is the numbers after the '.'.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 04-16-2018 at 21:14.
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-16-2018 , 21:43   Re: Mathematical Problem
Reply With Quote #4

Also, make sure that the values that you are dividing are floating-point values. I'm guessing that they are currently integers. So, if they are indeed integers, you'll need to use float() on them to make sure that you aren't doing integer division.
__________________
fysiks is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 04-16-2018 , 23:19   Re: Mathematical Problem
Reply With Quote #5

PHP Code:
new Float:porcentaje = (g_ammopacks[id] / levelsAmmopack(g_level[id])) * 100.0 
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 04-16-2018 at 23:20.
EFFx is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 04-17-2018 , 14:51   Re: Mathematical Problem
Reply With Quote #6

The problem is that you are dividing an integer with an integer just like fysiks said and after that you multiply by 100.
Here's a simple example to show you why it doesn't work:
Code:
    server_print("1: %.2f", 5/3*100.0); // 1: 100.00 (5/3 = 1, 1*100 = 100)     server_print("2: %.2f", 5*100.0/3); // 2: 166.66 (5*100 = 500, 500/3 = 166.66)

As you can see, as long as the multiplication is done before division the result is what you expect.

Here are the options you have.
Code:
    // Saved as integer, move the multiplication.     new intVar1 = floatround(1 * 100.0 / 3)     server_print("1: %.1f / %d", float(intVar1), intVar1); // 1: 33.0 / 33     // Saved as integer, turn it into float before dividing.     new intVar2 = floatround(float(1) / 3 * 100.0)     server_print("2: %.1f / %d", float(intVar2), intVar2); // 2: 33.0 / 33     // Saved as float, move the multiplication.     new Float:flVar3 = 1 * 100.0 / 3     server_print("3: %.1f / %d", flVar3, floatround(flVar3)); // 3: 33.3 / 33     // Saved as float, turn it into float before dividing.     new Float:flVar4 = float(1) / 3 * 100.0     server_print("4: %.1f / %d", flVar4, floatround(flVar4)); // 4: 33.3 / 33
I suggest the first option for simplicity.
__________________

Last edited by Black Rose; 04-17-2018 at 15:09.
Black Rose is offline
THC420
Junior Member
Join Date: Apr 2018
Old 04-18-2018 , 09:04   SOLVED Mathematical Problem
Reply With Quote #7

yes, that was the problem, thanks man
THC420 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:08.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode