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
7
Question by sdgd · Mar 05, 2013 at 07:59 PM · c#returnwrongcalculationdivide

why is this divide calculation returning me 0?

 float Percentage ;
 int CountFalsed = 0;
 int Falsed = 0;
         
         
 Percentage = ( (1-0) /121) /1; // means if everything is true I'm giving 0.008% to each 121 slot if it's falsed I'm giving more %
 Debug.Log("" + Percentage);

I went so far to the obvious

 Percentage = (1/2);

returned me same 0

but when I give it

 Percentage = (2/1);

returns me 2 at last ....

 Percentage = (2*0.5);

gives me 1 at last

what's the calculation I do not understand here?!?!?

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

2 Replies

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

Answer by Bunny83 · Mar 05, 2013 at 08:07 PM

Because you divide with integer values so it's an integer division. An integer division always returns an integer. the fractional part is always trucated. So 1 / 2 = 0.5 -> 0

You need to use float values. When you divide by constant numbers use a float literal:

 int V = 10;
 float P = V / 100.0f;    // P will be 0.1f

If you want to divide two integers and want a fractional part you have to cast one of the operands into a float:

 int V = 10;
 int D = 100; 
 float P1 = V / D;           // P1 will be 0.0f
 float P2 = (float)V / D;    // P2 will be 0.1f
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 sdgd · Mar 05, 2013 at 08:12 PM 0
Share

wow didn't know that thanks now I know why somewhere else I got some problems but it somehow worked but not as I wanted

avatar image Westland · Sep 24, 2018 at 11:18 PM 2
Share

In case someone else misses this (like I did) - If you're doing:

 image.fillAmount = (intA / intB);

To make it work it needs to be:

 image.fillAmount = ((float)intA / intB);

$$anonymous$$ight be other versions, still learning

avatar image
1

Answer by gregroberts · Dec 01, 2014 at 08:22 PM

This worked for me:

  int A = 10;
  int B = 100; 
 
  float P1 = A / B;             // P1 will be 0 int
  float P2 = (A * 1.0f / B);    // P2 will be 0.1f

Comment
Add comment · 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 Pangamini · Sep 30, 2015 at 09:40 AM 1
Share

you can just use the cast float P2 = ((float)A / B)

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

17 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

Unity seems to round small numbers to zero 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Object slows down when moving to a NavPoint, speed is not constant 1 Answer

NullReference 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