Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NinjaSquirrel · Dec 19, 2011 at 12:13 AM · stringfloatseed

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I create a list with an int/float and a string? C# 2 Answers

what the hell is a int? (or a float, or boolean, or string!) 3 Answers

Can a player change (int, string, float etc....) from outside the game? 3 Answers

X.Text just uses random values 1 Answer

Extracting list of floats from string C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges