PDA

View Full Version : float to int for credit system


lingzhidiyu
10-07-2014, 18:47
i am try to load Twincount and CTwincount from kv,here is my code.

new Float:WinCredit = Twincount / CTwincount; //they are all the float
RoundToNearest(WinCredit);
if (WinCredit > 100) {
WinCredit = 100;
} else if (WinCredit < 10) {
WinCredit = 10;
}

and this?

GetBaseCredit = StringToInt(BaseCredit);
new WinCredit = (Twincount / CTwincount) * GetBaseCredit; //they are all the int,how to make sure the result is int?
if (WinCredit > 100) {
WinCredit = 100;
} else if (WinCredit < 10) {
WinCredit = 10;
}

Mitchell
10-07-2014, 19:01
before dividing you might want to convert your ints individually to floats using:
float();
and convert floats back to ints using
RoundToZero()
RoundToCeil()
or
RoundToFloor()

lingzhidiyu
10-07-2014, 20:13
this?


StringToFloat(BaseCredit);
Float(Twincount);
Float(CTwincount);

new Float:WinCredit = (Twincount / CTwincount) * BaseCredit;
RoundToZero(WinCredit);

if (WinCredit > 100) {
WinCredit = 100;
} else if (WinCredit < 10) {
WinCredit = 10;
}

LambdaLambda
10-07-2014, 21:04
No. First of all, when you're converting variable that you want to be saved, then you need to create a new one:
Float(Twincount);
Float(CTwincount);
RoundToZero(WinCredit);
that is wrong.

And also, you want to use float(), not Float().

new Float:Twins = float(Twincount);
new Float:CTwins = float(CTwincount);
new WinCredits = RoundToZero(WinCredit);
that is correct.

Another thing is - you don't have to create a new Float just to use it for conversion only. What you can do is to create new int and save to it the Rounding function that will include your calculation:

new WinCredit = RoundToZero((float(Twincount) / float(CTwincount)) * BaseCredit);

Powerlord
10-08-2014, 11:09
You don't actually need to convert all the values to floats. Like other programming languages, SourcePawn will automatically upcast number types to make them compatible... in this case, if you try to multiply one Float and one integer (untagged variable), it will convert the integer to a Float first.