# How to round a float to two decimals?

**URL:** <https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [October 29, 2019, 4:19pm UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739 "2019-10-29T16:19:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [October 29, 2019, 4:19pm UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/1 "2019-10-29T16:19:55Z")

</div>

I want to round a float, but I want it to always have 2 numbers after the dot. even if its say 10.00, so it doesn’t mess the string. problem is that if I multiply by 100, integer and then divide by 100, if the number is .0 it doesn’t show the zeroes, and also after a while, the precision gets off and I start getting results like 10.0000446554 or so.

how do I do so?

---

<div class="post-metadata">

**Author:** ![Xeru98](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Xeru98](https://forums.unrealengine.com/u/Xeru98)\
**Post date:** [October 29, 2019, 5:58pm UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/2 "2019-10-29T17:58:16Z")

</div>

I believe this has already been answered here: [How can I set float precision? - Programming & Scripting - Unreal Engine Forums](https://answers.unrealengine.com/questions/94901/how-can-i-set-float-precision.html?lang=ja)

What you are doing is multiplying your number by the 10^number of point you want (in your case 2), using floor to cut off the tail and then dividing by that first number to get your formatted result

---

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [October 29, 2019, 8:33pm UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/3 "2019-10-29T20:33:27Z")

</div>

this answer doesn’t solve the fact that I want to force an integer to have 2 zero decimals. but there’s an answer above the best answer on that post that solves it with the “ToText (float)” node.

---

<div class="post-metadata">

**Author:** ![Lowenfas](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lowenfas](https://forums.unrealengine.com/u/Lowenfas)\
**Post date:** [April 7, 2023, 12:02am UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/4 "2023-04-07T00:02:25Z")

</div>

That doesn’t even work. It should and I can’t understand what the problem is but it still gives answers like 12.499998 and 30.100002

---

<div class="post-metadata">

**Author:** ![StevenHammon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/stevenhammon/32/88719_2.png) [@StevenHammon](https://forums.unrealengine.com/u/StevenHammon)\
**Post date:** [April 7, 2023, 2:01am UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/5 "2023-04-07T02:01:14Z")

</div>

If you convert a number to a float, you will get float variance. Float by it’s very definition is inaccurate. It’s like taking a digital sound wave and turning into analogue sound and then measuring a specific point and getting errors compared to the digital values. It’s like an analogue version of a number. You can’t convert to a float without getting some variance sometimes.

 ![1DecimalPlace](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/0/6/8064febb466323a645006bb80dcbbebb933d8b59.jpeg)

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [April 7, 2023, 3:21am UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/6 "2023-04-07T03:21:35Z")

</div>

This was just answered here, too:

> [@Need to round float to one decimal point. Why is it so difficult?](https://forums.unrealengine.com/t/need-to-round-float-to-one-decimal-point-why-is-it-so-difficult/866763/8):
>
> The reason is that floating point numbers aren’t decimal. It’s literally impossible to represent 0.1 exactly in a floating point number. Computer can’t do it. 0.5, 0.25, 0.125, 0.0625, and other numbers that are even fractions of 2 are exactly representable. The problem is that you’re doing this: Try to represent a value that’s exactly 0.1 Present the value that’s stored above exactly What you should be doing is: Represent whatever the number actually is Present the number with rounding …

Use `ToText (float)` to present a floating point number with a fixed number of decimal digits.

---

<div class="post-metadata">

**Author:** ![StevenHammon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/stevenhammon/32/88719_2.png) [@StevenHammon](https://forums.unrealengine.com/u/StevenHammon)\
**Post date:** [April 7, 2023, 4:22am UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/7 "2023-04-07T04:22:13Z")

</div>

> [@jwatte](#):
>
> ToText (float)

Yeah the problem is, if you NEED a float, that doesn’t help. Converting it back to a float to be able to use it, brings back the variance.

 ![FloatVariance](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/9/6/696e0812b3ae56a9aeb09bed8bb49b30fc6759ad.jpeg)

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [April 8, 2023, 1:36am UTC](https://forums.unrealengine.com/t/how-to-round-a-float-to-two-decimals/457739/8 "2023-04-08T01:36:45Z")

</div>

You never _need_ the float to be at exactly 0.1.  
If you need that, you should use integers, at 10x the resolution.  
What you _need_ when it comes to floats, is to _present it_ as if it’s at 0.1.

Fixed point is not a bad thing, and it’s not uncommon in things like combat system mechanics. Define anything in units of 1/1000, store it in an int, divide and round when presenting.

Btw, if you DO use floats, and you round when presenting, note that 0.99 will round to 1.0, but if you have some game mechanic that kicks in when you hit 1.0, the user will be confused why that mechanic doesn’t kick in.

Some programmers say that “you can’t trust floats” or “floats aren’t precise,” which isn’t actually true. However floats behave in a very special, well-defined way, which is _not the way_ that one might naively expect if one is used only to decimals, and fixed-point numbers (like you’d write them on graph paper or whatever.)
