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