How would I make the prices for the inventory items not use decimals using the tutorial from Unity RPG Shops & Abilities? I replaced all the floats to int that are named price, and it didn’t work.
Thank you,
Eric
How would I make the prices for the inventory items not use decimals using the tutorial from Unity RPG Shops & Abilities? I replaced all the floats to int that are named price, and it didn’t work.
Thank you,
Eric
What do you mean by “It didn’t work”? If the prices are integers, the won’t have decimals. However, we are using percentages (like discounts) and once calculated, those will result in floats. You can round those as you please to convert them back to integers
There are decimals and I don’t want them in my game. I need to learn how to convert it.
There are several functions available.
Because we are calculating percentages, you will get decimals. You have to decide how you would like to round those values.
Let’s take an example. We have a sword for 120 gold, and the shop will give you 2% discount. Now, for you the sword only costs 117.6 gold. But you don’t want the .6 decimal.
Using Mathf.RoundToInt(117.6)
will give you 118. RoundToInt()
rounds the value to the nearest integer. That is, 117.6 becaomes 118, but 117.3 would become 117. If it is .5 - exactly in the middle - it will round to the nearest integer that is even: 118.
Using Mathf.FloorToInt(117.6)
is the same as just dropping the decimals. It will always return the largest integer that is smaller than the value. That is 117.
Using Mathf.CeilToInt(117.6)
will always return the smallest integer that is larger than the value. That is 118.
So, these are your options. Just decide how you want to round the values, and give it a go
This is the method I use to keep the prices set to integer levels.
That work, I also want to have the prices set to int, so would I just replace float to int in every price variable and methods?
I would recommend keeping the prices as floats to start, then converting to int via the above methods when displaying them and calculating the final transactions.
This ensures that all discount percentages are treated as floats and not ints.
Yea I replaced all the floats with int and it is not what I want. I want to have the inventory items that have a price to display the price without the decimal. How would I do that? Correct me if I am wrong, I should look in the InventoryItem script?
I figured it out, Thank you @bixarrio and @Brian_Trotter, this post helped me. Thank you guys again, I very much appreciated it.
Eric.
Yeah I am surprised I figured it out relatively quickly. I knew that rounding those discounts would be a pain if it was ALL ints, so doing the rounding around the whole thing after applying discounts works. I am glad that I could help out indirectly