Why is this not working?
#1

i made a fuel system and it works great, if i make the fuel string show as floating point number (%f)
this shows the fuel as "100.0" ....when i make the string as integer (%i) it shows the fuel in the game as a big number, what the heck is causing this ? (2349992347)

how to make the fuel show as "100" without point

pawn Код:
public OnGameModeInit()
{
    for(new Vehicles = 0; Vehicles < MAX_VEHICLES; Vehicles++)
    {
        Fuel[Vehicles] = random(100);
    }
    return 1;
}

forward Speedometer();
public Speedometer()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
        {
            format(string,sizeof(string),"~w~Fuel: %i", Fuel[GetPlayerVehicleID(i)])
        }
    }
    return 1;
}
Reply
#2

Why dont you try using %d
Reply
#3

Quote:
Originally Posted by Shockey HD
Посмотреть сообщение
Why dont you try using %d
%i & %d are pretty much the same thing


Use %i with
pawn Код:
floatround(Fuel[GetPlayerVehicleID(i)])
Reply
#4

pawn Код:
forward Speedometer();
public Speedometer()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
        {
            format(string,sizeof(string),"~w~Fuel: %i",  floatround(Fuel[GetPlayerVehicleID(i)]));
        }
    }
    return 1;
}
Reply
#5

Simply use "%.0f" to omit the fractional part. Similarly, use "%.1f", "%.2f", "%.3f" to display one, two or three decimal digits respectively.
Reply
#6

thanks all for your replies, it worked with floatround, but why ? i still dont get it
Код:
floatround(Fuel[GetPlayerVehicleID(i)])
Reply
#7

Well, a float (%f, %.0f, %.2f...) is a number with decimal fractions included. For example: 5.43, 87.2, 93.59385, 2.00 etc.
However an integer (%i, %d) is just a number with no decimals, or whole numbers. For example: 1, 6, 245, 83 etc.

By adding a decimal value, you are no longer using an integer. 'floatround' will round the FLOAT to the nearest INTEGER. So it basically converts it from %f to %i.

Another example:
If I have a number that is 75.636, that is my current fuel. If I try to use %i, it will think it is an integer. However, as you now know, integers do NOT have decimal values, so the server will be putting a FLOAT into an INTEGER variable. Giving you some random massive number which I cannot currently explain at the moment because I don't understand why either, but it does.
However, if you use floatround, it will convert 75.636 to the nearest whole number, which is 76. Because 75.636 is closer to 76 than 75. So we now have an integer, which can be used properly with %i or %d.

I'm trying to explain it in the easiest way to understand, but I can't tell if I'm doing it right :P

Definition of a float/decimal: "a fraction whose denominator is a power of ten and whose numerator is expressed by figures placed to the right of a decimal point."
Definition of an integer: "a number which is not a fraction; a whole number."
Floatround: "Round a floating point number to an integer value." https://sampwiki.blast.hk/wiki/Floatround
Reply
#8

but i already made the fuel random(100), so will give a random number like "75" for example
"75" is an integer, so it should work with %i
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)