• 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
Question by Joneson Oh · Dec 29, 2009 at 10:25 AM · javascript-specific

Working with float variables in javascript

Hi all,

I've been trying to use float variables in my code below:

class CodingNode{

var f1aList = new Array(); var f1b; var activity : float = 0.0; var choiceValue = -1;

function CodingNode(){ Debug.Log("Coding node created"); this.activity = 0.0; Debug.Log("Activity = " + activity); choiceValue = 1/2; Debug.Log("Choice = " + choiceValue); }

}

However the Debug messages show "0" for both the variable activity and choiceValue, when they should be showing "0.0" for activity and "0.5" for choiceValue. Any help would be greatly appreciated.

Thanks

Comment
jashan
xortrox

People who like this

2 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Bampf · Dec 29, 2009 at 11:59 AM

When you write "1/2" you are dividing an integer by an integer. Many languages, including C and Unity JavaScript, assume that you want integer division; the result is therefore truncated to an integer. The fact that it is then assigned to a variable that was declared as a float makes no difference.

To get floating point division, either the numerator or denominator (or both) just needs to be a float.

Also note that Debug.Log does not put a trailing decimal ".0" on whole decimals. So you can't necessarily tell from the output whether a given round number is a float or not.

Comment
jashan
Ehren
Jin
cncguy
xortrox

People who like this

5 Show 1 · 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
avatar image Joneson Oh · Dec 29, 2009 at 03:42 PM 0
Share

hi your answer solved all my problems! i didn't know about the Debug.Log truncating the trailing .0 on decimals. Thank you so much. I can finally carry on with my work proper now.

avatar image

Answer by Ashkan_gc · Dec 29, 2009 at 11:53 AM

put 1.0/2.0 and then you will see the correct results. in javascript the variables are dynamic and their type can be changed. the problem is you are deviding 2 integer numbers. in C# you can use f as suffix "1f" to say the number is a float but i don't know can you do this in javascript or not. i am sure that if you use 1.0 and 2.0 it will give you 0.5 but i don't know it's a double or float. the problem surely is about number literals and is not about variable type because they are dynamic in javascript. hope this helps

Comment
jashan
Ehren
xortrox

People who like this

2 Show 1 · 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
avatar image Joneson Oh · Dec 29, 2009 at 03:41 PM 0
Share

thanks for your answer, it helped!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making an object clickable 2 Answers

grab all game objects with a distance of the object? 2 Answers

Calling function in a script based on raycast hit 1 Answer

Move a Person to a point / Repeat a function called from another script? 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