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 codeguyross1 · Dec 06, 2012 at 09:03 PM · c#mathfloatintegerdivide

C# divide float by integer

Hello, I need to divide a float by and integer. I have tried casting but nothing seems to work. I would like to see .25 as my result in the following problem.

0.5/2 = .25

Here is some examples of what I have tried that doesnt work and the result it gave. 0.5/((float)2) = 0 0.5/2.0 = 0 ((float).5)/((float) 2) = 0 (float).5/ 2 = 0

Thanks in advance!

Comment
Add comment · Show 2
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 Owen-Reynolds · Dec 06, 2012 at 10:38 PM 0
Share

Exactly how are you testing it? Because `Debug.Log(0.5/2.0);` correctly says 0.25, and `float x=0.5/2.0;` prints nothing -- it gives an error (double to float.)

Not a single one of the things you wrote should ever give 0.

avatar image codeguyross1 · Dec 06, 2012 at 10:46 PM 0
Share

I was looking at it in the watch section in the debug mode in $$anonymous$$ono. It doesn't seem to work very well. The answers provided by Jordan and Eric5h5 are both correct ways to do this.

2 Replies

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

Answer by Landern · Dec 06, 2012 at 09:07 PM

 //c#
 float result = 0.5f / 2;
 
 //js
 var result : float = 0.5f / 2;
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
avatar image
2

Answer by Eric5h5 · Dec 06, 2012 at 09:09 PM

That's how it works by default. As long as one of the numbers involved in an operation is floating point, then the result is floating point and you don't have to do anything special.

 void Start () {
     Debug.Log (0.5f/2);  // result is .25
 }
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 Max_Bol · Nov 08, 2020 at 04:47 PM 0
Share

This is an old answer, but as it lacks certain details, I'll add it as a comment. There are 2 rules within the operation flow that are to be taken into consideration.

  1. Brackets rules above everything else.

  2. Additions & Subtractions are isolators, like brackets.

What this means is that having only integers between brackets or Addition/Subtraction will results in integers-based values for those sub-operation.

(Int_A / Int_B) + Float_C x (Int_D / Float_E) is an formula that works to display the isolators effect. (Note that "x" is used for multiplication. It would be a italic text code otherwise.)

  • (Int_A / Int_B) will return the floored value as an integer.

  • (Int_D / Float_E) will return the proper float value as the float and integer are isolated together.

  • Float_C will be multiplied by the float result of (Int_D / Float_E).

So, if we fill the numbers like ( (int)1 / (int)2 ) + (float)3 x ( (int)4 / (float)5 ), the result will be 2.4f while, on paper, it should be 2.9f. That's because the ( (int)1 / (int)2 ) returns 0 and not 0.5f due to the isolating brackets being prioritized before the conversion to a float.

Changing the formula to ( (int)1 / (int)2 ) x (float)3 x ( (int)4 / (float)5 ) will result with 0.

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

13 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

Related Questions

Distribute terrain in zones 3 Answers

How to i set the int to hitpoints / 10 + the current int. 1 Answer

Why are floats not accessing my inputted values? 1 Answer

Floating Point and ToString issue 2 Answers

Float to int casting in C# script with modulo giving division by zero error 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