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 /
  • Help Room /
avatar image
0
Question by Tageos · Dec 16, 2015 at 10:26 AM · c#modulus

Check if a divided variable returns an integer

Hey!

I want to check if a variable divided by five returns an integer. I have this:

 if ((i/5) == "an integer" )

Appretiate all help, thank you!

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
2
Best Answer

Answer by Hellium · Dec 16, 2015 at 09:52 AM

Supposing i is an integer, then (i/5) will always return an integer (even if i = 4 for example)

When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2

If i is a float, then (i/5) will always return a float (even if i = 5.0f)

To obtain a quotient as a rational number or fraction, give the dividend or divisor type float or type double

I advise you to check if (i % 5) == 0, meaning that the remainder after division of i by 5 gives you 0, meaning that i is a multiple of 5

Source : https://msdn.microsoft.com/en-us/library/3b1ff23f.aspx

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 KdRWaylander · Dec 16, 2015 at 10:35 AM 0
Share

If you set 5.468284 as a value for an integer, your computer will only store 5 for the value because of the integer type that "takes nothing" after the point.

Hence, if the result of your maths is 0.8 (with i = 4 in your case), the system will only keep 0 if the output type is an integer. If it's a float it will keep the ".8" part.

Hellium gives you the right way of doing it, use the % operator :)

avatar image
0

Answer by gjf · Dec 15, 2015 at 11:18 PM

 if ((i/5) == Mathf.RoundToInt(i/5)) {... }
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
0

Answer by wibble82 · Dec 16, 2015 at 03:02 PM

If i is an integer variable, then i/5 will also be an integer - i.e. any remainder bits will be chopped off, because integer variables can only store whole numbers.

If i is an integer and what you actually want to know is "is i divisible by 5" then use the modulus (aka remainder) integer operator:

 //if the remainder of i/5 is 0...
 if((i%5)==0)
 {
 }

If i is a float, then the result may well contain a fractional part. Floating point values are not precise, so you can't easily check if the division was exactly an integer result. However you can get the fractional part and check if it is approximately 0:

 //do your division
 float my_division = i / 5.0f;
 
 //mathf.Round gets the closest whole number, so by subtracting round(my_division)
 //from my_division we are left with the fractional part
 float remainder = my_division - Mathf.Round(my_division);
 
 //now we can check if the remainder is approximately 0
 if(Mathf.Approximately(remainder,0))
 {
 }
 

That's the long winded way to write it! We could get it on fewer lines though:

     //or all on fewer lines...
     float my_division = i/5;
     if(Mathf.Approximately(my_division-Mathf.Round(my_division),0))
     {
     }
 

Or we could even put it in a function...

 bool IsAnInteger(float val)
 {
     return Mathf.Approximately(val-Mathf.Round(val),0);
 }
 
 bool IsDivisibleBy(float a, float b)
 {
     return IsAnInteger(a/b);
 }
 

:)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Button calls itself numerous times 0 Answers

C# Expression denotes a 'value', where a 'method group' was expected 1 Answer

how to implement multiplayer in Unity 2020.3.15f1 0 Answers

Enemy AI: Player attacke when in FOV 0 Answers

Making a timer on the press of a Key 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