- Home /
How do I find the amount of characters in a float?
I want to take a float and divide it by 10^charactersInFloat. This would allow me to convert random numbers like 3198075327985, to 3.198075327985.
Or is there a better way to convert a float to a number between 0-1? Thanks.
Answer by jahroy · Dec 19, 2011 at 12:40 AM
I've never used it, but a quick search yields this:
http://unity3d.com/support/documentation/ScriptReference/Random.html
It appears that Random.value is exactly what you want: a random number between 0 and 1.
/* print ten random numbers */
for ( var i = 0; i < 10; i ++ ) {
Debug.Log("random number #" + i + ": " + Random.value);
}
If you have to be able to handle large random numbers like you've listed, you can take the input number and divide it by 10 until it's less than 10 (or less than 1):
var inputNumber : float;
function Start ()
{
var normalizedNumber : float = normalize(inputNumber);
print("Here it is: " + normalizedNumber);
}
function normalize ( someFloat : float ) : float
{
while ( someFloat > 1 ) {
someFloat /= 10.0;
}
return someFloat;
}
Boy... there's gotta be a function out there that already does this.
Either way, it's good practice.
To really answer your original question, I'm pretty sure that's one thing you can use logarithms for: to find out how many numbers there are to the left of the decimal place in a number.
You could also convert the float to an int, then to a string, and count the characters.