Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by Infinity · May 23, 2011 at 07:10 AM · floatintdivide

Need decimals of a division of Ints

Hi,

I have two ints and dividing them I need to obtain a float. But if I use the normal division I obtain a int without decimals, what can I do?

Lot of 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

3 Replies

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

Answer by oxium · May 23, 2011 at 07:20 AM

just cast in float before you do the division. something like that :

 int i1 = 10;
 int i2 = 3;
 float f = ((float) i1) / ((float) i2);

then f will be equal to 3.33333 (instead of 3 if you change the type of f to be integer)

Comment
Add comment · Show 2 · 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 Infinity · May 23, 2011 at 08:46 AM 0
Share

It gives me this error:

Assets/nuevo/Scripts/Progressive Health Bar.js(35,28): UCE0001: ';' expected. Insert a semicolon at the end.

Exactly in : float f = ((float) i1) / ((float) i2);

avatar image Joshua · May 23, 2011 at 09:10 AM 0
Share

He is using C#, are you as well?

avatar image
3

Answer by KeithK · May 23, 2011 at 07:20 AM

Hey Infinity

What's happening is because you dividing an int by another int, it is assumed you would like an int as your answer type.

To get your result as a float, just cast your first int to a float like so:

 float answer = ((float)intOne) / intTwo;

This is then seen as a float being divided and so implicit conversions to float from all non-float types will occur.

Comment
Add comment · Show 4 · 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 Infinity · May 23, 2011 at 08:45 AM 0
Share

It gives me the following error when I use it:

Assets/nuevo/Scripts/Progressive Health Bar.js(35,28): UCE0001: ';' expected. Insert a semicolon at the end.

The error is exactly in this line

avatar image KeithK · May 23, 2011 at 08:51 AM 0
Share

Oh damn it. Sorry, forgot a set of brackets. Updated the answer even though this question is marked as answer.

avatar image Statement · May 23, 2011 at 09:01 AM 1
Share

I think it's because Infinity expects a JS solution while you and oxium provide C# solutions. I think you could just replace the initial types from int and float to var.

avatar image Infinity · May 23, 2011 at 12:51 PM 0
Share

And how I can use it on javascript? I can't change the main type of the variables >__<

avatar image
1

Answer by SisterKy · Jan 11, 2013 at 12:13 PM

This is an old question but since it was the first to come up when I googled I'd like to add the js-solution

 var c : float = parseFloat(a) / b;

or

 var c : float = (a+0.0) / b;


Additional Info:
Casting in UnityScript is usually done through "variable name" as "classname" ,
but since int/float are primitives, this WON'T work:

 a as float

Source (Answer by Eric, additional Info by Dreamora)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How do i divide int by float? 1 Answer

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

Whole Numbers Divide Incorrectly....BUG? 0 Answers

Are float values reliable? 0 Answers


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