# How can I set float precision?

**URL:** <https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [September 9, 2014, 6:09am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242 "2014-09-09T06:09:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon12961559](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon12961559](https://forums.unrealengine.com/u/anon12961559)\
**Post date:** [September 9, 2014, 6:09am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/1 "2014-09-09T06:09:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Pierdek](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pierdek](https://forums.unrealengine.com/u/Pierdek)\
**Post date:** [September 9, 2014, 6:19am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/2 "2014-09-09T06:19:22Z")

</div>

In BP or c++?

---

<div class="post-metadata">

**Author:** ![anon12961559](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon12961559](https://forums.unrealengine.com/u/anon12961559)\
**Post date:** [September 9, 2014, 6:25am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/3 "2014-09-09T06:25:06Z")

</div>

Blueprint.

---

<div class="post-metadata">

**Author:** ![Denny](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Denny](https://forums.unrealengine.com/u/Denny)\
**Post date:** [September 9, 2014, 8:28am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/4 "2014-09-09T08:28:31Z")

</div>

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.

 ![14894-floatprecisionexample.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/9/4/794942a3b6bee8cb52eca6b65f69c180ad51b53f.jpeg)

---

<div class="post-metadata">

**Author:** ![anon12961559](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon12961559](https://forums.unrealengine.com/u/anon12961559)\
**Post date:** [September 9, 2014, 10:08am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/5 "2014-09-09T10:08:41Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![kat0r](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kat0r](https://forums.unrealengine.com/u/kat0r)\
**Post date:** [September 9, 2014, 10:18am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/6 "2014-09-09T10:18:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon12961559](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon12961559](https://forums.unrealengine.com/u/anon12961559)\
**Post date:** [September 9, 2014, 11:29am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/7 "2014-09-09T11:29:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sylvator](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sylvator](https://forums.unrealengine.com/u/Sylvator)\
**Post date:** [June 17, 2016, 2:42pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/8 "2016-06-17T14:42:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Neilz0r](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Neilz0r](https://forums.unrealengine.com/u/Neilz0r)\
**Post date:** [August 27, 2016, 6:39am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/9 "2016-08-27T06:39:37Z")

</div>

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?) 🙂

Open image in a new tab to zoom.

 ![104526-ue4editor_2016-08-27_01-35-45.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/c/8/2c8632fad800db1021e741eeac42f94ce6f079dd.png)

---

<div class="post-metadata">

**Author:** ![Neilz0r](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Neilz0r](https://forums.unrealengine.com/u/Neilz0r)\
**Post date:** [August 29, 2016, 1:31pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/10 "2016-08-29T13:31:15Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a7dd0a2f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a7dd0a2f1](https://forums.unrealengine.com/u/anonymous_user_a7dd0a2f1)\
**Post date:** [October 11, 2016, 5:04am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/11 "2016-10-11T05:04:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Doublezer0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Doublezer0](https://forums.unrealengine.com/u/Doublezer0)\
**Post date:** [November 23, 2016, 3:45pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/12 "2016-11-23T15:45:09Z")

</div>

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/](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
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ab4bc0881](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ab4bc0881](https://forums.unrealengine.com/u/anonymous_user_ab4bc0881)\
**Post date:** [May 10, 2018, 9:45pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/13 "2018-05-10T21:45:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Motanum](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Motanum](https://forums.unrealengine.com/u/Motanum)\
**Post date:** [May 23, 2018, 4:35pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/14 "2018-05-23T16:35:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_056d0a8b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_056d0a8b](https://forums.unrealengine.com/u/anonymous_user_056d0a8b)\
**Post date:** [September 28, 2018, 9:22pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/15 "2018-09-28T21:22:45Z")

</div>

654.99 cannot be exactly represented as a float. The closest float value is 654.989990234375. See [https://onlinegdb.com/BkaMhGhYm](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.

---

<div class="post-metadata">

**Author:** ![LS\_Dehrk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LS\_Dehrk](https://forums.unrealengine.com/u/LS_Dehrk)\
**Post date:** [May 17, 2019, 5:37pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/16 "2019-05-17T17:37:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hkk2019UE4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hkk2019UE4](https://forums.unrealengine.com/u/hkk2019UE4)\
**Post date:** [September 7, 2019, 11:22am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/17 "2019-09-07T11:22:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BakonGuy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bakonguy/32/1000091_2.png) [@BakonGuy](https://forums.unrealengine.com/u/BakonGuy)\
**Post date:** [October 17, 2019, 10:58am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/18 "2019-10-17T10:58:31Z")

</div>

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

 ![290332-ue4editor-2019-10-17-05-56-09.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/8/2/682002e65fbbaca29766ab698f095bdd0a293ec2.png)

---

<div class="post-metadata">

**Author:** ![JunLietSun](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JunLietSun](https://forums.unrealengine.com/u/JunLietSun)\
**Post date:** [December 21, 2020, 5:03am UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/19 "2020-12-21T05:03:55Z")

</div>

![324942-qq截图20201216110607.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/7/5/1751356f1ae62c34d74f339f5c36f05cdaf52e9f.png)

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?

---

<div class="post-metadata">

**Author:** ![GunJudge](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GunJudge](https://forums.unrealengine.com/u/GunJudge)\
**Post date:** [May 5, 2021, 6:26pm UTC](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242/20 "2021-05-05T18:26:07Z")

</div>

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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/b/a/9ba317d99984c55eeadd03214ae8870489a6fd97.png)

[Next page](https://forums.unrealengine.com/t/how-can-i-set-float-precision/293242.md?page=2)
