Easily convert UMG Text Input from user to float!
Dear Community,
While I was trying to convert numbers inputted into Editable Text Blocks in UMG, I ran into the following:
- user enters 10000
- I convert Text input to string, and then to float, and get 10000
- user enters 10,000, I do the above but I get 10 in code instead.
10?!!!
I was highly confused
Well turns out that when FString gets 10,000 which is converted that way by FText, then comma truncates the FString conversion to float and I end up with 10.
would be compounded by culture setting where user is inclined to enter 10.000,00
So!
I hereby present you with nodes that convert from FText to Float and Int, with Culture support!
See pics!
**Culture Support**
As you can see in my pics I support both 10,000.99 and 10.000,99.
If anyone has other versions like 10'000.99 that they need support for, let me know!
The option to switch is hidden by default for convenience, just like for print string and FText to number conversions.
Enjoy!



C++ Code
Here is the C++ code for these nodes!
Please note the FText’s are always passed by const reference for efficiency reasons.
Also note that my internal FString formatting code uses ReplaceInline which avoids creating a copy of the string just to replace the symbols, and is thus much more efficient code.
UFUNCTION(BlueprintPure, Category = "VictoryBPLibrary")
static bool **Text_IsNumeric**(const FText& Text)
{
return Text.IsNumeric();
}
UFUNCTION(BlueprintPure, Category = "VictoryBPLibrary", meta=(AdvancedDisplay = "1"))
static float **Text_ToFloat**(const FText& Text, bool UseDotForThousands=false)
{
//because commas lead to string number being truncated, FText 10,000 becomes 10 for FString
FString StrFloat = Text.ToString();
TextNumFormat(StrFloat,UseDotForThousands);
return FCString::Atof(*StrFloat);
}
UFUNCTION(BlueprintPure, Category = "VictoryBPLibrary", meta=(AdvancedDisplay = "1"))
static int32 **Text_ToInt**(const FText& Text, bool UseDotForThousands=false)
{
//because commas lead to string number being truncated, FText 10,000 becomes 10 for FString
FString StrInt = Text.ToString();
TextNumFormat(StrInt,UseDotForThousands);
return FCString::Atoi(*StrInt);
}
static void **TextNumFormat**(FString& StrNum, bool UseDotForThousands)
{
//10.000.000,997
if(UseDotForThousands)
{
StrNum.**ReplaceInline**(TEXT("."),TEXT("")); //no dots as they truncate
StrNum.**ReplaceInline**(TEXT(","),TEXT(".")); //commas become decimal
}
//10,000,000.997
else
{
StrNum.**ReplaceInline**(TEXT(","),TEXT("")); //decimal can stay, commas would truncate so remove
}
}
New Download (29mb, Media Fire)
Please note my downloads also include these packaged binaries:
- Win64 Development
- Win32 Shipping
- HTML5 Development
Please see my instructions for Packaging UE4 Plugins With Your Game.
Donations can be sent to me via: