Answers for "how to round a float to 2 DP"
http://answers.unity.com/questions/50391/how-to-round-a-float-to-2-dp.html
The latest answers for the question "how to round a float to 2 DP"Answer by mrstruijk
http://answers.unity.com/answers/1907718/view.html
I know this is an old question, but since it's the top Google result I wanted to update it a bit.
----------
I was having problems with rounding when it came to when the latest to-round-digit was a 5. It sometimes (but not with every value!) did not round *up*, but *down*.
For instance, if I'm rounding to 1 decimal:
float rounded = Math.Round(1.45f, 1);
# rounded = 1.5
Which is correct and what I wanted.
However:
float rounded = Math.Round(1.55f, 1);
# rounded = 1.5
Is *not* correct...! This has to do with the MidpointRounding enum value, and needing to cast a float to a decimal.
----------
I wrote an extension method, which seems to solve the issues, based on [Stackoverflow user RahulTripathi's comment][1] and [docs][2]
/// <summary>
/// This rounds a float to specified decimals.
/// It solves issues when rounding some .5 values.
/// </summary>
/// <param name="originalValue"></param>
/// <param name="decimals"></param>
/// <returns></returns>
public static float RoundCorrectly(this float originalValue, int decimals)
{
decimal originalAsDecimal = (decimal) originalValue; // Float needs to be cast to decimal.
const MidpointRounding midwayRounding = MidpointRounding.AwayFromZero;
var rounded = (float) Math.Round(originalAsDecimal, decimals, midwayRounding);
return rounded;
}
[1]: https://stackoverflow.com/questions/37290845/incorrect-result-of-math-round-function-in-vb-net+
[2]: https://docs.microsoft.com/en-us/dotnet/api/system.midpointrounding?redirectedfrom=MSDN&view=net-6.0Sat, 18 Jun 2022 14:33:57 GMTmrstruijkAnswer by Retruate
http://answers.unity.com/answers/1144379/view.html
I know this is an old post, but I simply use`Mathf.Ceil(myVariable * 10) / 10 //add a '0' to each 10 to increase the decimal places`
It's simple and it works.Fri, 19 Feb 2016 04:52:09 GMTRetruateAnswer by ephraimdov
http://answers.unity.com/answers/1004053/view.html
try these .....
decimal d = 100.123456M;
decimal dc = Math.Round(d, 2);
d.ToString("#.##");
String.Format("{0:0.00}", d);
More info.....[Round a decimal value to 2 decimal places][1]
Dov
[1]: http://net-informations.com/q/faq/round.htmlFri, 10 Jul 2015 06:21:45 GMTephraimdovAnswer by estefanowolf
http://answers.unity.com/answers/832157/view.html
//create a double
//create a float
float a;
double b;
b = System.Math.Round(a,2);
// replace number 2 with number of decimals you wish to round upto.Sat, 15 Nov 2014 09:24:47 GMTestefanowolfAnswer by charmandermon
http://answers.unity.com/answers/509497/view.html
just add this at the top:
using System;
It will give you access to math.round
Then do something cool like this:
speedCapped = (float)Math.Round((double)lerpedSpeed,2);
Its a little over the top but cool.Wed, 07 Aug 2013 05:07:11 GMTcharmandermonAnswer by masterarcher
http://answers.unity.com/answers/50397/view.html
<p>i foudn this example in the end on stackoverflow.:</p>
<p>decimal a = 1.994444M;</p>
<p>Math.Round(a, 2); //returns 1.99</p>
<p>decimal b = 1.995555M;</p>
<p>Math.Round(b, 2); //returns 2.00</p>
<p>i think this is a little neater than yours Mike, cheers for the reply tho.</p>Thu, 03 Mar 2011 16:06:02 GMTmasterarcherAnswer by yoyo
http://answers.unity.com/answers/50396/view.html
<p>To get time in thousandths of a second, you could do this (C# syntax) ...</p>
<pre><code>float seconds = Time.time;
int thousandths = (int)(seconds * 1000.0f);
</code></pre>
<p>The C# system library includes <a href="http://msdn.microsoft.com/en-us/library/75ks3aby.aspx" rel="nofollow">Math.Round</a>, which lets you pass in the number of decimal places you want to preserve. However it works with double and decimal numbers, which may not be desirable. Unity's <a href="http://unity3d.com/support/documentation/ScriptReference/Mathf.Round.html" rel="nofollow">Mathf.Round</a> does not let you specify decimal places (feature request??), but you can implement your own version like this:</p>
<pre><code>public static float Round(float value, int digits)
{
float mult = Mathf.pow(10.0f, (float)digits);
return Mathf.Round(value * mult) / mult;
}
</code></pre>
<p>Note that it will be more efficient to just perform the computation inline with your code, knowing the appropriate power of 10, for example:</p>
<pre><code>float rounded = Mathf.Round(seconds * 1000.0f) / 1000.0f;
</code></pre>
<p>or (if you don't care about rounding):</p>
<pre><code>float rounded = (int)(seconds * 1000.0f) / 1000.0f;
</code></pre>
<p>Finally, if this is just for formatting and display, not calculation, then there are several alternatives shown in <a href="http://answers.unity3d.com/questions/31014/timers-tenths-of-seconds-visible-digits-issue" rel="nofollow">this question</a>.</p>Thu, 03 Mar 2011 16:05:08 GMTyoyoAnswer by Mike 3
http://answers.unity.com/answers/50395/view.html
<p>If it's just for changing to string:</p>
<pre><code>yourFloat.ToString("F2")
</code></pre>
<p>If you want the float itself truncated:</p>
<pre><code>yourFloat = Mathf.Round(yourFloat * 100f) / 100f;
</code></pre>Thu, 03 Mar 2011 16:04:17 GMTMike 3