# float repair??

**URL:** <https://discussions.unity.com/t/float-repair/909659>\
**Category:** Scripting\
**Tags:** Scripting, Question\
**Created:** [February 18, 2023, 4:56pm UTC](https://discussions.unity.com/t/float-repair/909659 "2023-02-18T16:56:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 18, 2023, 4:56pm UTC](https://discussions.unity.com/t/float-repair/909659/1 "2023-02-18T16:56:54Z")

</div>

everything ok works… but sometimes I’m doing some tests but during the upgrade I get numbers like 1.0000002 it’s not possible to be more precise about the increase??? in this case I am giving some precision 0.3

```csharp
if (selectPlayer == 0)
        {
            if (PickUpCollected.goldDiamond >= costDamagePl1)
            {
                {
                    StatsPlayer.actualDamagePl1 += 0.3f;
                    PickUpCollected.goldDiamond -= costDamagePl1;
                    costDamagePl1 = costDamagePl1 + 3;
                    PlayerPrefs.SetInt("GoldDiamond", PickUpCollected.goldDiamond);
                    PlayerPrefs.SetInt("CostDamagePl1", costDamagePl1);
                    PlayerPrefs.SetFloat("ActualDamagePl1", StatsPlayer.actualDamagePl1);
                }
            }
        }

```

as you can see I ask to increase by 0.3f but the last strange number that came out is 6.099998 Why?? can it be solved??

---

<div class="post-metadata">

**Author:** ![Chubzdoomer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/chubzdoomer/32/96287_2.png) [@Chubzdoomer](https://discussions.unity.com/u/Chubzdoomer)\
**Post date:** [February 18, 2023, 5:12pm UTC](https://discussions.unity.com/t/float-repair/909659/2 "2023-02-18T17:12:28Z")

</div>

[https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html](https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html)

---

<div class="post-metadata">

**Author:** ![DevDunk](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/devdunk/32/901008_2.png) [@DevDunk](https://discussions.unity.com/u/DevDunk)\
**Post date:** [February 18, 2023, 5:52pm UTC](https://discussions.unity.com/t/float-repair/909659/3 "2023-02-18T17:52:36Z")

</div>

That’s how floats work. They are reasonably precise, and pretty fast, but not perfect.  
Using a double (precision) variable is a lot more precise, but takes up more memory and performance in return

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 18, 2023, 5:58pm UTC](https://discussions.unity.com/t/float-repair/909659/4 "2023-02-18T17:58:02Z")

</div>

> [@Chubzdoomer](#):
>
> [https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html](https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html)

Thank you for your answer it’s a very long article a bit heavy and tiring to fully understand… you want it to depend on the topic… you want me to go through the translator… anyway you can read it… I understood that Mathf.Approximately could solve my problem… but I don’t understand how I could use them in my case…

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 18, 2023, 6:01pm UTC](https://discussions.unity.com/t/float-repair/909659/5 "2023-02-18T18:01:52Z")

</div>

> [@DevDunk](#):
>
> That’s how floats work. They are reasonably precise, and pretty fast, but not perfect.  
> Using a double (precision) variable is a lot more precise, but takes up more memory and performance in return

so with double solves… I’m not interested in performance here… I have to change a lot of code in this case… can a variable inherent to damage be double?? why then the variable “public static float bulletDmg;” will accept to be equal to the double??? because I already had to make many variables static to allow for changes…

---

<div class="post-metadata">

**Author:** ![DevDunk](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/devdunk/32/901008_2.png) [@DevDunk](https://discussions.unity.com/u/DevDunk)\
**Post date:** [February 18, 2023, 6:10pm UTC](https://discussions.unity.com/t/float-repair/909659/6 "2023-02-18T18:10:01Z")

</div>

You can cast to a float if it’s within the float range

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 18, 2023, 6:53pm UTC](https://discussions.unity.com/t/float-repair/909659/7 "2023-02-18T18:53:22Z")

</div>

> [@DevDunk](#):
>
> You can cast to a float if it’s within the float range

Would you be so kind as to give me an example?

---

<div class="post-metadata">

**Author:** ![halley](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/halley/32/102322_2.png) [@halley](https://discussions.unity.com/u/halley)\
**Post date:** [February 18, 2023, 6:58pm UTC](https://discussions.unity.com/t/float-repair/909659/8 "2023-02-18T18:58:25Z")

</div>

Just accept the precision for what it is. Don’t try to use double variables, it’s just going to snowball and complicate everything, yet it’s just delaying the inevitable. You can’t describe a base-10 fraction in binary numbers without some discrepancies in the least-significant digits.

If you want to check if X is approximately equal to Y, use `Mathf.Approximately(X, Y)`. It’s basically saying the same thing as `Mathf.Abs( (X - Y) ) < someTinyAmountCalledEpsilon`.

Experiment with this online toy to explore float precision: [IEEE-754 Floating Point Converter](https://www.h-schmidt.net/FloatConverter/IEEE754.html) – in particular, enter 0.3 in the “You Entered” box, from your example.

---

<div class="post-metadata">

**Author:** ![Yoreki](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/yoreki/32/155895_2.png) [@Yoreki](https://discussions.unity.com/u/Yoreki)\
**Post date:** [February 18, 2023, 7:17pm UTC](https://discussions.unity.com/t/float-repair/909659/9 "2023-02-18T19:17:46Z")

</div>

Im with @halley1 here. **Why** do you have a problem with this precision problem?  
What is your actual usecase, where this is problematic?

---

<div class="post-metadata">

**Author:** ![halley](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/halley/32/102322_2.png) [@halley](https://discussions.unity.com/u/halley)\
**Post date:** [February 18, 2023, 7:27pm UTC](https://discussions.unity.com/t/float-repair/909659/10 "2023-02-18T19:27:37Z")

</div>

> [@Yoreki](#):
>
> Im with @halley here. **Why** do you have a problem with this precision problem?  
> What is your actual usecase, where this is problematic?

Given their example, it looks like an accounting error. Banks avoid all of this by using very specific rounding rules at every operation (see: COBOL, BCD, string-encoding, etc.). When you want to present things to the user, just round it off. Discrepancies in the 7th decimal are not going to be interesting to players.

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 19, 2023, 9:06am UTC](https://discussions.unity.com/t/float-repair/909659/11 "2023-02-19T09:06:03Z")

</div>

> [@Yoreki](#):
>
> Im with @halley1 here. **Why** do you have a problem with this precision problem?  
> What is your actual usecase, where this is problematic?

> [@halley1](#):
>
> Given their example, it looks like an accounting error. Banks avoid all of this by using very specific rounding rules at every operation (see: COBOL, BCD, string-encoding, etc.). When you want to present things to the user, just round it off. Discrepancies in the 7th decimal are not going to be interesting to players.

as you can see in the example… pressing a button increases a player’s statistic by 0.3f… a variable then … will show the current statistic … and it’s bad to see that for example the data are 0.8000001432 I would like to show in that variable simply 0.8 without the remainder

---

<div class="post-metadata">

**Author:** ![Bunny83](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@Bunny83](https://discussions.unity.com/u/Bunny83)\
**Post date:** [February 19, 2023, 10:05am UTC](https://discussions.unity.com/t/float-repair/909659/12 "2023-02-19T10:05:34Z")

</div>

> [@PlayerCasual](#):
>
> and it’s bad to see that for example the data are 0.8000001432 I would like to show in that variable simply 0.8 without the remainder

In your last post you quoted halley:

> [@halley1](#):
>
> When you want to present things to the user, just round it off.

If you want to round a number to 1 decimal place, just multiply it by 10, round it and divide by 10 again. Also when you actually convert a number to a string, the ToString method can take a format string where you can specify how many digits should be shown.

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 19, 2023, 10:30am UTC](https://discussions.unity.com/t/float-repair/909659/13 "2023-02-19T10:30:07Z")

</div>

and how do i do this??? programmatically how should I write??

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 19, 2023, 10:51am UTC](https://discussions.unity.com/t/float-repair/909659/14 "2023-02-19T10:51:31Z")

</div>

> [@halley1](#):
>
> Given their example, it looks like an accounting error. Banks avoid all of this by using very specific rounding rules at every operation (see: COBOL, BCD, string-encoding, etc.). When you want to present things to the user, just round it off. Discrepancies in the 7th decimal are not going to be interesting to players.

could you post me an example or a link of how to round???

---

<div class="post-metadata">

**Author:** ![Neto\_Kokku](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/neto_kokku/32/99214_2.png) [@Neto\_Kokku](https://discussions.unity.com/u/Neto_Kokku)\
**Post date:** [February 19, 2023, 10:57am UTC](https://discussions.unity.com/t/float-repair/909659/15 "2023-02-19T10:57:36Z")

</div>

If by “want to show” you mean “display the value on the screen” then you can round a float to a specific number of decimal places using ToString():

> **[C# Online Compiler | .NET Fiddle](https://dotnetfiddle.net/10cFDl)**
>
> Test your C# code online with .NET Fiddle code editor.

---

<div class="post-metadata">

**Author:** ![Leuki](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/leuki/32/295328_2.png) [@Leuki](https://discussions.unity.com/u/Leuki)\
**Post date:** [February 19, 2023, 10:57am UTC](https://discussions.unity.com/t/float-repair/909659/16 "2023-02-19T10:57:53Z")

</div>

Here are some examples:

Round to the nearest integer:

```csharp
float numberToRound = 4.6f;
int roundedNumber = Mathf.RoundToInt(numberToRound);
Debug.Log(roundedNumber); // Output: 5

```

In this example, the Mathf.RoundToInt function takes a float value numberToRound, and rounds it to the nearest integer. The resulting value is stored in the roundedNumber variable, which is then printed to the console using Debug.Log.

1. Round to a specified number of decimal places:

```csharp
float numberToRound = 4.6789f;
float roundedNumber = Mathf.Round(numberToRound * 100) / 100.0f;
Debug.Log(roundedNumber); // Output: 4.68

```

In this example, the Mathf.Round function is used to round a float value numberToRound to two decimal places. The number is first multiplied by 100 to move the decimal point two places to the right, then rounded to the nearest integer using Mathf.Round, and then divided by 100.0 to move the decimal point back two places to the left. The resulting value is stored in the roundedNumber variable, which is then printed to the console using Debug.Log.

Note that in the second example, you can change the number of decimal places you want to round to by changing the factor you multiply by in the Mathf.Round function. For example, to round to three decimal places, you would multiply by 1000 instead of 100.

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 19, 2023, 11:09am UTC](https://discussions.unity.com/t/float-repair/909659/17 "2023-02-19T11:09:47Z")

</div>

> [@Leuki](#):
>
> Here are some examples:
> 
> Round to the nearest integer:
> 
> ```csharp
> float numberToRound = 4.6f;
> int roundedNumber = Mathf.RoundToInt(numberToRound);
> Debug.Log(roundedNumber); // Output: 5
> 
> ```
> 
> In this example, the Mathf.RoundToInt function takes a float value numberToRound, and rounds it to the nearest integer. The resulting value is stored in the roundedNumber variable, which is then printed to the console using Debug.Log.
> 
> 1. Round to a specified number of decimal places:
> 
> ```csharp
> float numberToRound = 4.6789f;
> float roundedNumber = Mathf.Round(numberToRound * 100) / 100.0f;
> Debug.Log(roundedNumber); // Output: 4.68
> 
> ```
> 
> In this example, the Mathf.Round function is used to round a float value numberToRound to two decimal places. The number is first multiplied by 100 to move the decimal point two places to the right, then rounded to the nearest integer using Mathf.Round, and then divided by 100.0 to move the decimal point back two places to the left. The resulting value is stored in the roundedNumber variable, which is then printed to the console using Debug.Log.
> 
> Note that in the second example, you can change the number of decimal places you want to round to by changing the factor you multiply by in the Mathf.Round function. For example, to round to three decimal places, you would multiply by 1000 instead of 100.

thank you for your answer… but so in my case how could I do???  
why the variable “public static float actualDamagePl1;”  
and in a file and the visualization takes place in another file… “StatsPlayer”  
trying with  
“damageTxt.text = Mathf.Round(StatsPlayer.actualDamagePl2 \* 100) / 100.0f”  
I point it all out as a mistake… “cannot implicitly convert type float to string”

---

<div class="post-metadata">

**Author:** ![spiney199](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/spiney199/32/880897_2.png) [@spiney199](https://discussions.unity.com/u/spiney199)\
**Post date:** [February 19, 2023, 11:34am UTC](https://discussions.unity.com/t/float-repair/909659/18 "2023-02-19T11:34:20Z")

</div>

> [@PlayerCasual](#):
>
> I point it all out as a mistake… “cannot implicitly convert type float to string”

You just need to convert it to a string with `.ToString()` that every object has.

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 19, 2023, 12:03pm UTC](https://discussions.unity.com/t/float-repair/909659/19 "2023-02-19T12:03:52Z")

</div>

> [@spiney199](#):
>
> You just need to convert it to a string with `.ToString()` that every object has.

but where???  
damageTxt.text = Mathf.Round(StatsPlayer.actualDamagePl2.ToString() \* 100) / 100.0f; error  
damageTxt.text = Mathf.Round(StatsPlayer.actualDamagePl2 \* 100) .ToString() / 100.0f; error

---

<div class="post-metadata">

**Author:** ![PlayerCasual](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@PlayerCasual](https://discussions.unity.com/u/PlayerCasual)\
**Post date:** [February 19, 2023, 12:10pm UTC](https://discussions.unity.com/t/float-repair/909659/20 "2023-02-19T12:10:56Z")

</div>

ok i solved…  
frist use in start metodh  
StatsPlayer.actualDamagePl2 = Mathf.Round(StatsPlayer.actualDamagePl2 \* 100) / 100.0f;  
then where i need  
damageTxt.text = StatsPlayer.actualDamagePl2.ToString();  
tnk to all
