- Home /
Why is there sometimes an f behinf a number?
Hello, I'm following a few different tutorials on unity and scripting and i noticed that some put an f behind an integer or float when they are assigning it to a variable and I was wondering what the use of it is and why some people place an f and some don't.
$$anonymous$$ost things that work in C# work in Javascript. After all, they both compile down to the same thing. Please convert your comments to answers, as they do answer the question
Ok, we've been told ;)
You are officially my 'Awesome dude of the day'
Edit : see, I knew there was a reason I made a comment on a technical question I didn't have the exact answer for, I cannot compete with a C# pro, and all-round pro Eric ..
Thnx everyone! I already knew a bit of the basics of js before starting with unity, but i had never seen the f before, but know i know why and what use it has. All answers are good answers in my opinion, but i just picked one to make sure that it is beeing showed as answered.
Answer by Eric5h5 · Mar 25, 2013 at 07:57 AM
Both Unityscript and C# use "f" to indicate a float and "d" to indicate a double. The difference is that double is the default for C#, but float is the default for Unityscript. So if you use a decimal number but leave off the "f" or "d", then it will use the default for whatever language you're using.
Answer by robertbu · Mar 24, 2013 at 02:52 PM
The 'f' is needed in C# because it has two different types of floating point numbers: float and double. C#'s default floating point type is double, but everything in Unity is a float, so the f is needed to force the compiler to treat a number like 3.286 as a float instead of a double.
Answer by AlucardJay · Mar 24, 2013 at 02:18 PM
The f is for a floating point number in C#. So you are seeing it in C#, and not in uJS. Make sure you know the difference between the two. Here's some links I found useful in converting between C# and JS. Read the first one especially, as they will explain the differences :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
http://www.unifycommunity.com/wiki/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?
http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/
Edit : decided that I would leave the 'double' explanation to a C# person =]
@alucardj - C# that's me. I'm forever looking stuff up (or trying it out) when I answer questions with Javascript. Note I found out awhile back by accident that the 'f' suffix works in Javascript as well:
function Start()
{
var f = 3.14159f;
Debug.Log("f = "+f);
}
It looks like Javascript/Unityscript has a 'd' for double and 'f' for float, but defaults to float.