- Home /
Convert Object to a Float
How do you convert an object (such as a Hashtable element) to a float?
Is there a method better than parseFloat(obj.ToString())
, where obj
is the object you want converted to float?
ina you often see this as a workaround
var aFloat:float;
aFloat = 1.0 * someInt;
often you see for example
pixelWidthFloat = 1.0 * texture.width;
Answer by Kryptos · Jan 19, 2012 at 01:12 PM
In C# you can cast with the value-cast operator:
float myFloat = (float) obj;
But this only works if you initially put a float into the Hashtable (or any weak-typed collection). If it was a string, then no there is no other way (actually you could write implicit operator extensions but I discourage to do it).
The method in the snippet I posted in the question works for any format.
There is no such thing as a value-cast operator in UnityScript (as only works for reference-type).
you're really better to ask on stackoverflow about general program$$anonymous$$g, casting, etc questions
Your answer
Follow this Question
Related Questions
creating and manipulating a grid efficiently 1 Answer
How to make an object float in space 4 Answers
how to save an array into an object 1 Answer
Keeping a float variable within a range based on keyboard input 1 Answer
JS objects in Unityscript || BCE0048: Type 'Object' does not support slicing. 0 Answers