The Divide sign is not dividing?
#1

Hi!

I have this script:

pawn Код:
format(fuelStr[GetPlayerVehicleID(playerid)],256,"Fuel: %i%%", vehicleFuel[GetPlayerVehicleID(playerid)]/400000*100); // This Format Our Text Into What We See
    TextDrawSetString(fdisplay[playerid], fuelStr[GetPlayerVehicleID(playerid)]); // This Changes The Value Of Our Textdraw To What We Formatted
    if(s2[playerid] != 0) {
    vehicleFuel[GetPlayerVehicleID(playerid)] = (vehicleFuel[GetPlayerVehicleID(playerid)]-(s2[playerid]*5/18));
    }
    printf("Currently going %d | Fuel %d Left | Fuel Percentage: %d | %d/400000*100", s2[playerid], vehicleFuel[GetPlayerVehicleID(playerid)], vehicleFuel[GetPlayerVehicleID(playerid)]/400000*100, vehicleFuel[GetPlayerVehicleID(playerid)]);
It's supposed to remove fuel depending on the speed of a player, all the results are correct, it removes the fuel correctly but when I convert it to a percentage it messes up.
pawn Код:
vehicleFuel[GetPlayerVehicleID(playerid)]/400000*100
I have no idea what could be going wrong because when I look at the printf that I created there, it gives me this:

Currently Going 50 | Fuel 399980 | Fuel Percentage 0 | 399980/400000*1000
Which when I enter in to my calculator, gives me the correct answer of 99.995

I've tried changing it to a float variable but that didn't help, did I possibly do something wrong?

Kind Regards

Matthew

P.S. s2[playerid] is the Speed at which the player is going by the way.
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
No, that's the right answer - you are doing integer division:

399980 / 400000 = 0.99995 AS A FLOAT, but integer divison ALWAYS rounds down so you get 0 * 100 = 0.

The simplest way to solve this is to rearrange your equation:

pawn Код:
vehicleFuel[GetPlayerVehicleID(playerid)] * 100 / 400000
That way you get 39998000 / 400000 = 99.995 as a float, or 99 as an integer.
Thank you very much, it works one hundred percent now. So there is absolutely no way to round up with integers?
Reply
#3

Thanks a lot, I appreciate your help, I can now stick my hair back in.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)