❔ How To Convert an Int to A Float

How can I convert a int to a float? This isn't working.
44 Replies
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
If you have an answer explain why, I want to learn
JansthcirlU
JansthcirlU16mo ago
alright, can you also include which error message you get?
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
JansthcirlU
JansthcirlU16mo ago
okay, let's start by removing the (float), just leave the assignment in there and tell me what the error says
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
Am I not using (float) right?
JansthcirlU
JansthcirlU16mo 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
samisaskinnyqueenOP16mo ago
Wdym?
JansthcirlU
JansthcirlU16mo ago
if you hover over the method name, what tooltip appears?
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
I fixed it I think
JansthcirlU
JansthcirlU16mo ago
oh actually I'm dumb sorry you need to remove the =
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
Just added a separate variable that is the Cost converted to a float Why woudl this do anything?
JansthcirlU
JansthcirlU16mo ago
the type of Cost is a float right?
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
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
JansthcirlU16mo ago
okay no matter, let's take back a step
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
*= would set the cost, and I wanted to get the product
JansthcirlU
JansthcirlU16mo ago
you have a cost, you want to increase it by 20% and then round it to an integer, right
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
yes
JansthcirlU
JansthcirlU16mo 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
samisaskinnyqueenOP16mo ago
So I was right, nice
JansthcirlU
JansthcirlU16mo 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
samisaskinnyqueenOP16mo ago
Did I not assign it?
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
JansthcirlU
JansthcirlU16mo ago
ah that's where you do it, yeah I couldn't see that from the previous screenshots
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
Srry lol Anyways, ty for the help
JansthcirlU
JansthcirlU16mo 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
samisaskinnyqueenOP16mo ago
Does setting it to an int automatically round it
JansthcirlU
JansthcirlU16mo ago
oh wait not quite, it always rounds down or more precisely, it just removes everything after the decimal point
amio
amio16mo ago
Like I said, why not (int)Math.Round(Cost * 1.2)? That should allow you to specify rounding, even, if that matters
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
how can i specify where it rounds to
JansthcirlU
JansthcirlU16mo ago
ye I should've taken a peek
amio
amio16mo ago
I think there are overloads for Round?
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
It doesn't fully matter if it rounds up or down so im fine
amio
amio16mo ago
Yeah, it takes specific arguments for decimals and midpoint rounding Then just Round should be fine
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
For the funnies, could I know the specific arguments
amio
amio16mo ago
Math.Round Method (System)
Rounds a value to the nearest integer or to the specified number of fractional digits.
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
Thanks
JansthcirlU
JansthcirlU16mo ago
I hope that works in Unity too, though I don't see why not
amio
amio16mo 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
JansthcirlU16mo ago
seems strange that they included their own Mathf library
samisaskinnyqueen
samisaskinnyqueenOP16mo ago
It should be the same, no?
amio
amio16mo ago
Probably not - the default should be in the docs
JansthcirlU
JansthcirlU16mo 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
Accord16mo 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