Check if Float equal "Number"
#1

I want to check if a Float equal "Number.00" or "Number.25", for example "1.75"..
pawn Код:
for(new i=0; i<100; i++)
{
    if(Float == N.00/* || usd == 'N.25' || usd == "N.50" || usd == 'N.75'*/)
    {
Reply
#2

You can get the fractional part of a float, then compare

floatfract
Reply
#3

Am I doing it right ?
pawn Код:
if(floatfract(Float) == 25)
Reply
#4

No, you should compare with a decimal part, like this:

pawn Код:
if(floatfract(3.50) == 0.50)
        printf("floatfract rules!");
Reply
#5

pawn Код:
if(floatfract(Float) != 0.00 || floatfract(Float) != 0.25 || floatfract(Float) != 0.50 || floatfract(Float) != 0.75) return SendClientMessage(playerid, -1, "Invalid amount");
Is that right ? Cause It's not working..
Reply
#6

No, you don't do it like that at all.

Just do this:

pawn Код:
if (num == 1.75)
You can also use floatcmp.
Reply
#7

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
No, you don't do it like that at all.

Just do this:

pawn Код:
if (num == 1.75)
You can also use floatcmp.
Then I'll have to make it for every number, Like 0.25, 0.50, 0.75, 1.00, 1.25, 1.50, etc
I'm sure there is a better/easier way...
Reply
#8

You should have used && (and) not or, or you just invent it like that
pawn Код:
new
    Float: frac = floatfrac(Float)
;
if(!(fract == 0.00 || fact == 0.25 || fract == 0.50 || fract == 0.75))
    return SendClientMessage(playerid, -1, "Invalid amount");
// also I am not sure if that also works or is faster
if(floatfrac(floatfrac(Float) / 0.25) == 0.0)
    return SendClientMessage(playerid, -1, "Invalid amount");
Reply
#9

Quote:
Originally Posted by xVIP3Rx
Посмотреть сообщение
Then I'll have to make it for every number, Like 0.25, 0.50, 0.75, 1.00, 1.25, 1.50, etc
I'm sure there is a better/easier way...
Oh, sorry. Try this then:

pawn Код:
stock floatmultiple(Float:number, Float:dest)
{
    while (number >= dest)
        number -= dest;

    return (!number);
}
And then:

pawn Код:
if (floatmultiple(num, 0.25))
{
    // ...
}
Because modulus operators don't work with floats, so that's the only way.
Reply
#10

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
You should have used && (and) not or, or you just invent it like that
pawn Код:
new
    Float: frac = floatfrac(Float)
;
if(!(fract == 0.00 || fact == 0.25 || fract == 0.50 || fract == 0.75))
    return SendClientMessage(playerid, -1, "Invalid amount");
// also I am not sure if that also works or is faster
if(floatfrac(floatfrac(Float) / 0.25) == 0.0)
    return SendClientMessage(playerid, -1, "Invalid amount");
pawn Код:
new Float:frac = floatfract(usd);
printf("FRACT : %f", frac);
if(!(frac == 0.00 || frac == 0.25 || frac == 0.50 || frac == 0.75)) return SendClientMessage(playerid, -1, "Invalid amount");
It's not working, returns "Invalid amount", And it prints nothing but some sscanf warning

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
Oh, sorry. Try this then:

pawn Код:
stock floatmultiple(Float:number, Float:dest)
{
    while (number >= dest)
        number -= dest;

    return (!number);
}
And then:

pawn Код:
if (floatmultiple(num, 0.25))
{
    // ...
}
Because modulus operators don't work with floats, so that's the only way.
I think you still didn't get me, Or I don't understand that code very well

I want to give a float to a player, which will be "Any Number.00" Or "AnyNumber.25" and so on, I thought there is a way to get the numbers after the "." and try to compare them.. If there isn't any, Then I can use a loop through like 50/100 numbers and compare the numbers after the "."..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)