❔ How To Convert an Int to A Float

How can I convert a int to a float? This isn't working.
44 Replies
samisaskinnyqueen
If you have an answer explain why, I want to learn
JansthcirlU
JansthcirlU11mo ago
alright, can you also include which error message you get?
JansthcirlU
JansthcirlU11mo ago
okay, let's start by removing the (float), just leave the assignment in there and tell me what the error says
samisaskinnyqueen
Am I not using (float) right?
JansthcirlU
JansthcirlU11mo ago
(float) is for converting a value, but you're doing an assignment so they don't work together okay, next up, can you show me what the Mathf.RoundToInt function does?
samisaskinnyqueen
Wdym?
JansthcirlU
JansthcirlU11mo ago
if you hover over the method name, what tooltip appears?
samisaskinnyqueen
I fixed it I think
JansthcirlU
JansthcirlU11mo ago
oh actually I'm dumb sorry you need to remove the =
samisaskinnyqueen
Just added a separate variable that is the Cost converted to a float Why woudl this do anything?
JansthcirlU
JansthcirlU11mo ago
the type of Cost is a float right?
samisaskinnyqueen
No Its an int thats why i am rounding it There isnt an error anymore when I remove the = tho is it bc im not setting the Cost
JansthcirlU
JansthcirlU11mo ago
okay no matter, let's take back a step
samisaskinnyqueen
*= would set the cost, and I wanted to get the product
JansthcirlU
JansthcirlU11mo ago
you have a cost, you want to increase it by 20% and then round it to an integer, right
samisaskinnyqueen
yes
JansthcirlU
JansthcirlU11mo ago
so the value you want to round is going to be Cost * 1.2, not Cost *= 1.2 the first gives you the value, the second is an assignment
samisaskinnyqueen
So I was right, nice
JansthcirlU
JansthcirlU11mo ago
exactly so here's where you need to decide if you want to assign the new value to Cost or just work with Cost * 1.2 and if you do want to assign, you'll have to do it outside of the RoundToInt method
samisaskinnyqueen
Did I not assign it?
JansthcirlU
JansthcirlU11mo ago
ah that's where you do it, yeah I couldn't see that from the previous screenshots
samisaskinnyqueen
Srry lol Anyways, ty for the help
JansthcirlU
JansthcirlU11mo ago
no worries, but anyways there's an easier way to do this as Amio said in #chat you can simply do this: Cost = (int)(Cost * 1.2f);
samisaskinnyqueen
Does setting it to an int automatically round it
JansthcirlU
JansthcirlU11mo ago
oh wait not quite, it always rounds down or more precisely, it just removes everything after the decimal point
amio
amio11mo ago
Like I said, why not (int)Math.Round(Cost * 1.2)? That should allow you to specify rounding, even, if that matters
samisaskinnyqueen
how can i specify where it rounds to
JansthcirlU
JansthcirlU11mo ago
ye I should've taken a peek
amio
amio11mo ago
I think there are overloads for Round?
samisaskinnyqueen
It doesn't fully matter if it rounds up or down so im fine
amio
amio11mo ago
Yeah, it takes specific arguments for decimals and midpoint rounding Then just Round should be fine
samisaskinnyqueen
For the funnies, could I know the specific arguments
amio
amio11mo ago
Math.Round Method (System)
Rounds a value to the nearest integer or to the specified number of fractional digits.
samisaskinnyqueen
Thanks
JansthcirlU
JansthcirlU11mo ago
I hope that works in Unity too, though I don't see why not
amio
amio11mo ago
The defaults should be pretty suitable then. hyper's int cast could too, you just need to know it truncates instead of any particular rounding logic
JansthcirlU
JansthcirlU11mo ago
seems strange that they included their own Mathf library
samisaskinnyqueen
It should be the same, no?
amio
amio11mo ago
Probably not - the default should be in the docs
JansthcirlU
JansthcirlU11mo ago
there's a few differences in what you can do using C# in Unity and what .NET allows you to do, but I wouldn't worry about that could be a cool read-up if you're interested but otherwise don't worry about it
Accord
Accord11mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts