How to format convert int32 to FString?

Like the title, say if I have a number “3”, the convert result should be “003”, if I have a number “33”, the convert result should be “033”, so on and so forth.
Right now I have a simple implementation like below

FText UFPWeaponInfo::IntToFText(int32 Num)
{
	// TODO: Optimize the logic so it doesn't look as stupid as now.
	if (Num >= 100)
	{
		return FText::FromString(FString::FromInt(Num));
	}
	else if (Num >= 10)
	{
		return FText::FromString(FString("0") + FString::FromInt(Num));
	}
	else
	{
		return FText::FromString(FString("00") + FString::FromInt(Num));
	}
}

This works but it’s just too “stupid”, is there any better way to do this like a Format or something?

1 Like

Hi there!
you can use FString::Printf to format the integer with leading zeros.
I created function and this should solve your problem

FText UFPWeaponInfo::IntToFText(int32 Num)
{
FString FormattedNum = FString::Printf(TEXT("%03d"), Num);
return FText::FromString(FormattedNum);
}

if I made any mistake in understanding your problem, please let me know

1 Like

Cool, this is exactly what I’m looking for, thank you!

1 Like

The proper way of doing it is using the FString member FString::FromInt(int). e.g.:

FString myString = FString::FromInt(30);