
Designed by OfficialJesseP
Hi PlayerSquared, today I am posting some code that many script developers will find useful. The below code will convert any float value to a string, allowing you to join it with other characters, or add it to a sentence. Anyone is welcome to use it, I just ask for you to please credit me.
ASM
Code:
:FloatToString_ //max of 9 decimal places
Function 2 11 0
PushString ""
pStatic FtoS
StrCopy 23
getF1 0//input
FtoI
Dup
setF1 5//left number
getF1 0//input
fPush_0.0
JumpGE @FloatToStringNoCorrectionNeeded_
getF1 0//input
fPush_-1
FCmpLE
JumpTrue @FloatToStringNoCorrectionNeeded_
PushString "-"
pStatic FtoS
StrAdd 23
:FloatToStringNoCorrectionNeeded_
pStatic FtoS
StrAddi 23
getF1 1//decimal places
JumpFalse @EndFloatToString_
PushString "."
pStatic FtoS
StrAdd 23
getF1 0//input
getF1 5//left number
ItoF
fSub
Dup
fPush_0.0
JumpGE @FlostToStringSetRemainder_
fPush_-1
fMul
:FlostToStringSetRemainder_
setF1 6//remainder
PushString "1"
pFrame1 7//multiplier
StrCopy 11
push_0
setF1 10
:FloatToStringCalculateMultiplier_
getF1 10
getF1 1//decimal places
JumpEQ @FloatToStringContinue_
push_0
pFrame1 7//multiplier
StrAddi 11
getF1 10
push_1
Add
setF1 10
Jump @FloatToStringCalculateMultiplier_
:FloatToStringContinue_
pFrame1 7//multiplier
pFrame1 11
CallNative "STRING_TO_INT" 2 1
Drop
getF1 11
ItoF
getF1 6//remainder
fMul
FtoI
pFrame1 7//right number
ItoS 10
getF1 1//decimal places
pFrame1 7//right number
CallNative "GET_LENGTH_OF_LITERAL_STRING" 1 1
Sub
Dup
setF1 10
JumpFalse @FloatToStringAddRightNumber_
:FloatToStringFixRightNumber_
getF1 10
JumpFalse @FloatToStringAddRightNumber_
push_0
pStatic FtoS
StrAddi 23
getF1 10
push_1
Sub
setF1 10
Jump @FloatToStringFixRightNumber_
:FloatToStringAddRightNumber_
pFrame1 7//right number
pStatic FtoS
StrAdd 23
:EndFloatToString_
pStatic FtoS
Return 2 1
Code:
Static FtoS[22] = 0;
C
Code:
char FtoS[22];
char *FloatToString(float input, int decimalPlaces) //max of 9 decimal places
{
int leftNumber = input;
float remainder;
char universal[10];
int i;
int multiplier;
strcpy(FtoS, "", 23);
if (input < 0.0f && input > -1.0f)
stradd(FtoS, "-", 23);
straddi(FtoS, leftNumber, 23);
if (decimalPlaces != 0)
{
stradd(FtoS, ".", 23);
remainder = input - leftNumber;
if (remainder >= 0.0f)
goto NoCorrectionNeeded;
remainder = remainder * -1.0f;
NoCorrectionNeeded:
strcpy(universal, "1", 11);
while (i != decimalPlaces)
{
straddi(universal, 0, 11), i++;
}
STRING_TO_INT(universal, &multiplier);
strcpy(universal, "", 10);
straddi(universal, multiplier * remainder, 10);
for (i = decimalPlaces - GET_LENGTH_OF_LITERAL_STRING(universal); i != 0; i = i - 1)
{
straddi(FtoS, 0, 23);
}
stradd(FtoS, universal, 23);
}
return FtoS;
}
Last edited by a staff member: