Answer the question
In order to leave comments, you need to log in
What is the best way to round a float?
I know the topic is hackneyed, but where I don’t look at the solution and everywhere the solution goes by turning it into a decimal and then into a float, as a result, I get these two methods, one float rounds, the other string float rounds, that is, methods return from float or string rounded float
public float FloatRoundFromFloat(float f){
decimal d = (decimal)Math.Round(f, 4); f = (float)d;
return f;
}
public float FloatRoundFromString(string s){
decimal d = Math.Round(Convert.ToDecimal(s.Replace(',', '.')), 4);
float f = (float)d;
return f;
}
Answer the question
In order to leave comments, you need to log in
Why use decimal is not clear
Return a float to get what, instead of 0.0004 = 0.0003999999?
My "rounding code" from a Unity project (example):
public float countdown = 60;
if (countdown > 0)
{
countdown -= Time.deltaTime;
timerText.text = countdown.ToString();
timerText.text = Mathf.Round(countdown).ToString();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question