How can I set float precision?

I have a float value (health percentage) that I’m sending to the HUD. However it comes through with many decimal places (ie. 95.6764387) and I want to reduce the precision to 2 (ie. 95.68 as per the previous example). How would I do this?

1 Like

In BP or c++?

Blueprint.

Here are three ways I could come up with. I think the string solution at the top should work best, so make it a function.

I thought there would be an existing function for this, doesn’t seem to be though.

Open the image in a separate tab to see it in full size.

9 Likes

Thanks so much! Great workarounds. There really should be a Float Precision node that you can pass floats through and set number of decimal places with an int input. I made a function from the string solution, and it works, but it’s cumbersome using a function that must be added into the chain of events (for lots of different values I’m sending to the HUD).

You could simply write your own BluePrintCallable function that uses C++ format strings.

I know python and MEL but not C++ unfortunately. Blueprints are awesome but I guess I need to start learning C++ to unleash the full power of Unreal.

Hey, just out of curiosity isn’t the middle workaround better? As its just a series of math functions which should be faster than conversions correct?

After much testing, I determined that none of these options truly work. When using the “ToText (float)” (aka, the Currency node) blueprint node, a value of 654.987976 with a precision of 2 decimal places will show up as “654.98999”. This is pretty ■■■■ accurate, but still not accurate enough if you’re relying on absolute mathematical precision.

I tried all of the above methods, and none seem work as well as the “ToText (float)” option. Also, “grouping” needs to be “unchecked”, otherwise a value of “2156466.5” will be “2.0”. So in other words, the top voted answer in this post is actually wrong.

[Click to Zoom][1]

Better solution:

This is the most accurate and actually corrects a mistake posted by the top rated answer in this post. The “AsCurrency” node is deprecated, so using the “ToText (float)” node is a good alternative. Also, “Use Grouping” needs to be unchecked, otherwise a value such as “2156466.5” will get rounded to “2.0” (Strange, huh?) :slight_smile:

Open image in a new tab to zoom.

12 Likes

For anyone who stumbles upon this post as I did. You probably already tried the math approach and got some weird non precision number. UE4 for some reason returns a number that is 0.000001 below the expected result.

However there is good news, while it does take a bit more CPU to do the String array conversion method does work.

Edit: Should be noted though the converting back to a float in the string array or the toText example will cause the same issue as the Math example. So you are stuck with using the float as a string if you want the precision to remain intact.

I have fixed previous floating point accuracy issues using std:setprecision from c++.
This tends to be on a case by case basis though and I have yet to do a global change in the engine as I think I will break too much along the way. Anyway here is the c++ page for a fix.

http://www.cplusplus.com/reference/iomanip/setprecision/

Code

// setprecision example
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision

int main () {
  double f =3.14159;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';
  std::cout << std::fixed;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';
  return 0;
}

Output

3.1416
3.14159
3.14159
3.141590000

You could just use “Round” which would give you the nearest integer if you’re just trying to simplify.

If you are using the top solution and integers, you want to use instead Get Character Array from String.

654.99 cannot be exactly represented as a float. The closest float value is 654.989990234375. See https://onlinegdb.com/BkaMhGhYm and hit ‘run’. ToText(float) with value of 654.987976 and precision of 2 decimal places correctly results in the text “654.99”. If you convert this text into a string and then back into a float, you end up with 654.989990234375, which is depicted as “654.98999” in the blueprint editor.

1 Like

There is a “round” note now! Please mark this to help others!

0.123456 * 1000 => round => divide by 1000 => 0.123

Here’s a quick blueprint function I made for this. I put it in a function library so it’s accessible everywhere.

3 Likes

if you are using C++, and you want to keep two decimal place, you can use the attribute of (float to int precision loss).
And the codes seem easy enough to understand right?

1 Like

I got a way that works every time for me so far. I convert it to text then just chop off the part past the amount of decimal places i want to keep. In this case 2 and use a period as the substring where i count decimal places from.

337668-image-2021-05-01-081507.png