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 Crispinator · Jan 11, 2015 at 11:52 PM · variableintintegerdivide

Divisible by 3

I need to take an int variable that that will never be the same number, and check to see if it is divisible by 3. If it is not, I need to find the nearest number that is. Any suggestions?

Comment
Add comment · Show 4
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 bodec · Jan 12, 2015 at 12:14 AM 0
Share

if you know that it needs to be divisible by 3 you can set a array to hold all the possibilities then run a random selection from the array

avatar image bodec · Jan 12, 2015 at 12:15 AM 0
Share

you can even use a for loop to fill the array

avatar image Baste · Jan 12, 2015 at 12:38 AM 0
Share

Are you generating the random numbers yourself, or just checking if it's divisible by 3? You know about the % operator, right?

avatar image edwinsoho · Jan 12, 2015 at 12:42 AM 0
Share

you can use a loop that changes a variable and check it every interaction but... my instinct says whatever you are trying to archive there is a better way than dividing by 3 and find closest number. Why don't you explain what you are trying to do?

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by KMKxJOEY1 · Jan 12, 2015 at 01:17 AM

 if(integer % 3 != 0) //this is if the integer is not divisible by 3
 {
     integer += integer % 3; //this will add the remained of /3, therefor finding the next multiple
 }
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 Kiwasi · Jan 12, 2015 at 02:12 AM 1
Share

Nice work on the %. But your implementation is slightly off. If should be

 if(integer % 3 != 0) {
     integer -= integer % 3; 
 }

Or if we want to ceil ins$$anonymous$$d of floor

 if(integer % 3 != 0) {
     integer += 3 - integer % 3; 
 }

Ideally you would wrap the whole thing in a method call, make the hard coded 3 an operator, and implement rounding.

avatar image
0

Answer by Kiwasi · Jan 12, 2015 at 02:14 AM

You could also use

 newInt = oldInt / 3 * 3;

This solution will round towards zero.

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

30 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 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

Pass a function(float) as variable 2 Answers

Displaying variable on UI text every frame (JS) 1 Answer

Script not changing int variable 2 Answers

Subtract from Integer 3 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