Float as String With Precision

From Epic Wiki
Jump to: navigation, search

Overview

Author: Rama (talk)

Here is my function for obtaining a float as a string, but limiting the number of decimal places.

 And including appropriate rounding!

Thank You Epic

Please note I am leveraging all the hard work of Epic Engineers by using the conversion functions provided with the FText class.

Thank You Epic!

Static

I recommend making your own static library of functions to use anywhere in your code, if you are just wanting to test stuff just remove the word static and put this in any .h file of your choosing

Code For You

//In YourFunctionLibrary.h

//Float as String With Precision!
static FORCEINLINE FString GetFloatAsStringWithPrecision(float TheFloat, int32 Precision, bool IncludeLeadingZero=true)
{
        //Round to integral if have something like 1.9999 within precision
	float Rounded = roundf(TheFloat);
	if(FMath::Abs(TheFloat - Rounded) < FMath::Pow(10,-1 * Precision))
	{       
		TheFloat = Rounded;
	}
	FNumberFormattingOptions NumberFormat;					//Text.h
	NumberFormat.MinimumIntegralDigits = (IncludeLeadingZero) ? 1 : 0;
	NumberFormat.MaximumIntegralDigits = 10000;
	NumberFormat.MinimumFractionalDigits = Precision;
	NumberFormat.MaximumFractionalDigits = Precision;
	return FText::AsNumber(TheFloat, &NumberFormat).ToString(); 
}

//Float as FText With Precision!
static FORCEINLINE FText GetFloatAsTextWithPrecision(float TheFloat, int32 Precision, bool IncludeLeadingZero=true)
{
        //Round to integral if have something like 1.9999 within precision
	float Rounded = roundf(TheFloat);
	if(FMath::Abs(TheFloat - Rounded) < FMath::Pow(10,-1 * Precision))
	{       
		TheFloat = Rounded;
	}
	FNumberFormattingOptions NumberFormat;					//Text.h
	NumberFormat.MinimumIntegralDigits = (IncludeLeadingZero) ? 1 : 0;
	NumberFormat.MaximumIntegralDigits = 10000;
	NumberFormat.MinimumFractionalDigits = Precision;
	NumberFormat.MaximumFractionalDigits = Precision;
	return FText::AsNumber(TheFloat, &NumberFormat); 
}

Include Leading Zero

Please note in my function you can opt to have a value of 0.5 display as

 .5

or

 0.5

Based on whether you want that leading zero or not!

Yay!

Example Usage

const float MyFloat = 16.16621111111111111111;

FString Str = "My Float is ";
Str += UYourFunctionLibrary::GetFloatAsStringWithPrecision(MyFloat,2);
ClientMessage(Str);

Output

 My Float is 16.17

More Info on C++ Static Function Libraries

My Wiki On Static Function Libraries https://wiki.unrealengine.com/Static_Function_Libraries,_Your_Own_Version_of_UE4_C%2B%2B,_No_Engine_Compile_Times


Summary

I hope you enjoy this function!

Rama (talk)