Looking for nearest increment of 10
Hi I have a piece of code that generates a random receipt value ( eg. 17.60 $). Now I want a piece of code that looks for the nearest increment of 10 based on that receipt value. ( 20$ in my case)
How would I do such a thing? I was thinking about using modulo, but I'm not quite sure how that would look.
thanks in advance.
Answer by Atenrev · Jul 29, 2019 at 05:35 PM
You could think about doing something like this
if (n > 1000)
{
n = Mathf.RoundInt(n/1000)*1000;
}
else if (n > 100)
{
n = Mathf.RoundInt(n/100)*100;
}
else if (n > 10)
{
n = Mathf.RoundInt(n/10)*10;
}
else if (n > 5)
{
n = 10;
}
else
{
n = 0;
}
But I don't find it too elegant. If I think of something better, I'll comment it.
Answer by Gilles_aerts · Jul 29, 2019 at 06:40 PM
I've done something simular and used the mathF.round function. that seemed to have done the trick, thank you.
Incase anyone wants to know what I did:
customerPay = Mathf.RoundToInt((receiptValue / 10)) * 10 ; if (customerPay < receiptValue) { customerPay += 10; }