Round float to specific decimal place

I have checked UE docs, but I haven’t found a way to round a float to a specific decimal place. I would like to print to the log my float with only two decimal places (90.00).

1 Like

It doesn’t round but it sounds like this is what you’re after for your use case
https://docs.unrealengine.com/en-US/API/Runtime/Core/Containers/FString/SanitizeFloat/index.html

FString Value = FString::SanitizeFloat(SomeFloat, 2);

Thank you, this looks like this will work.

This does work for adding zeros(I asked for in my question above), but it doesn’t get rid of numbers. If I have 90.134578 and I want to have 90.13 by specifying 2 as the second argument/parameter it does not work.

Thanks for the link to docs.

Duh, completely forgot formatting is a thing.

FString Value = FString::Printf("%.2f", 3.14159265359);
// Value == "3.14"

Thank you.

I have been trying to use FString::Printf("%.2f", CurrentYaw) but i keep getting an error that my format specifier must be a const TChar. I hovered over my text and it says it is a const char. I could not find any way to change it to a TChar so I just made a const TChar variable equal to “%.2” and put that in for my first parameter, but that didn’t work either. I did some research and saw that some people use FString::Format(), but I could not find a way to convert my CurrentYaw to a TArray without making another variable.

Oh sorry (again :man_facepalming:) I forgot the text macro.

FString Value = FString::Printf(TEXT("%.2f"), 3.14159265359);

Thank you very much for all the quick responses :grinning:. It now works.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms