- Home /
Is there a setting to change integer rounding to 'Swedish Rounding'?
Hi all,
Just to quickly preface this question, Swedish Rounding is a system where you round decimals to the nearest integer. So 5.3 will be rounded to 5, and 5.7 will be rounded to 6.
In Unity, I've found that if you have two numbers, one an int, and the other a float, and you make the int equal the float, the decimal value is rounded down no matter what(so 5.3 will equal 5, and so will 5.7). Basically it's like any number after the decimal point is ignored. Is there a setting in Unity to change this, or will I have to script something like this every time I want to round this way:
var inputNumber : float;
private var outputNumber : int; private var xInt : int; private var remainder : float;
function Update() { xInt = inputNumber; remainder = inputNumber - xInt; if (remainder >= 0.5) { outputNumber = inputNumber+1; } else { outputNumber = inputNumber; }
print(outputNumber);
}
Thanks for your help!
Answer by d3coy · Jan 03, 2011 at 04:25 AM
Mathf.Round is what you need.
http://unity3d.com/support/documentation/ScriptReference/Mathf.Round.html
Answer by SpookyCat · Jan 03, 2011 at 10:45 AM
Or add 0.5f to the float value int val = fval + 0.5f;
Your answer
Follow this Question
Related Questions
OpenFeint float scores 2 Answers
[Closed] Cannot convert types? 2 Answers
Double Round Problems 1 Answer
Checking an increasing number 1 Answer
I dont want Decimals :P 1 Answer