Convert "Double" to "Float" or "Int"?
I searched around but nothing useful came. Just some help about javascript (about parseFloat), but I'm using C# and it does not seem to work.
I have a private double which I want to divide by 360 and then use as angle. Problem is quaternions need floats, it doesn't work with double.
Any help on how to achieve this?
Thanks in advance.
Exactly. In C# you have to give number literals an "f" suffix so it's actually a float. In UnityScript it's reverse. Un US the default type is float, in C# it's double.
These linen of code (which are valid in both languages) produce different variables depending on the language:
var Var1 = 1.0;
var Var2 = 1.0f;
var Var3 = 1.0d;
In C# Var1 and Var3 would be of type double. Var2 would be a float.
In UnityScript Var1 and Var2 will be a float, Var3 would be a double.
However, i prefer to specify the type of the variables:
//C#
double Var1 = 1.0;
float Var2 = 1.0f;
double Var3 = 1.0d;
//UnityScript
var Var1 : float = 1.0;
var Var2 : float = 1.0f;
var Var3 : double = 1.0d;
Thanks so much!!! this really helped. i dont know why i didn't think of this before lol
Answer by fafase · Jul 27, 2012 at 09:16 AM
I do not use C# but you could try:
float variable = (float)yourDoubleVariable;
Edit: Information for other reader:
UnityScript allows to cast without any typecasting like
var aFloat:float;
var aInt:int;
aInt= aFloat;
will work and the conversion will simply truncate the result, not round so 1.9 becomes 1.
P.S:For those of you wondering how to use larger data than float in UnityScript you need to include the System.
var aDouble : System.Double;
var aBigInt : System.Int64;