• 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 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
SaturnoMichael
marquizzo
d2clon

People who like this

3 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

3 Replies

  • Sort: 
avatar image
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
Infinity
Mnescat
Andreas Hagen
jmasinteATI
SaturnoMichael
Classic-Niall
d2clon
HeroesHour

People who like this

8 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

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
Infinity
Schnodahipfe
marquizzo
EDevJogos

People who like this

4 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

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
Wolfram

People who like this

1 Show 0 · 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

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

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

Problems comparing float with a Mathf.RoundToInt() version of itself 1 Answer

Adding coin score to multiplied value 2 Answers

How to submit float score as time to Game Center? 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