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 El Maxo · Mar 25, 2015 at 03:44 PM · c#math

How to detect if number has a decimal point or not

Hi I am currently trying to find out weather a number is odd or even, to do this I have divided it by two and want to see if it has a decimal point or not. The problem is i'm not 100% sure how I would achieve that.

My code is bellow if i am not explaining it well enough.

using UnityEngine; using System.Collections;

public class Math : MonoBehaviour {

 decimal Number = 3;
 decimal number2 = 2;

 // Use this for initialization
 void Start () {
     Number = Number / number2;
     print (Number);
     if (Number Has a decimal Point){
         dosomething;

     }

 
Comment
Add comment · Show 1
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 Hrungdak · Mar 26, 2015 at 11:36 AM 0
Share

Your question title is confusing.

2 Replies

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

Answer by Reder13 · Mar 25, 2015 at 03:51 PM

you need to use the mod operator %

this gives you the remainder of two numbers

so number % number2 == 0 if it is even and number % number2 != 0 means odd

Comment
Add comment · Show 5 · 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 El Maxo · Mar 25, 2015 at 04:10 PM 0
Share

I am a bit lost, how would I put this number into context

avatar image Reder13 · Mar 25, 2015 at 04:55 PM 0
Share

I would do something like this:

float Number = 3;

// Use this for initialization void Start () {

print (Number % 2);

if ( Number % 2 != 0){ // if Number has a decimal

//dosomething;

}

you dont really need to have Number2 for your code since you know its going to be a 2

the code above prints out the remainder of Number and 2 (1 in this case) and since 1 isnt equal to 0 Number must be odd thus it enters the body of the if statement to do something.

avatar image Bonfire-Boy · Mar 25, 2015 at 05:19 PM 0
Share

@Reder13 the comment should say "if Number is odd" rather than "if Number has a decimal".

avatar image Reder13 · Mar 25, 2015 at 08:36 PM 0
Share

Yes and no in the current context yes your right it would be if Number is odd @Bonfire Boy. However I was thinking if Number were say 3.37 or some other value other than an int then it would be true if number has a decimal.

Thanks for re$$anonymous$$ding me to clarify that.

In the case you want to use Interger values only for Number just change the : float Numbers to int Numbers and the comment to // if Number is odd

avatar image Bonfire-Boy · Mar 26, 2015 at 12:40 AM 0
Share

@Reder13 I beg your pardon. The question talks about odd and even, so I had integers in my head and missed the fact that the code was all (strangely?) not using integers. Thanks for the clarification.

avatar image
2

Answer by Bonfire-Boy · Mar 26, 2015 at 01:14 AM

There are actually two questions here.

"Odd" and "even" are concepts that only apply to integers.

One way to test whether a number is odd or even is as Reder13 says, to use the mod operator. But it's only when you're dealing with integers, that you can do it with a single test...

 EParity OddOrEven( int i )
 {
     return ( i % 2 == 0 )?( EParity.Even ):( EParity.Odd );
 }  

But note that this only works because for integers, i%2 can only take the values 1 and 0.

Alternatively, and probably faster, you could just look at the ones bit

 EParity OddOrEven2( int i )
 {
     return ( ( i & 1 ) == 0 )?( EParity.Even ):( EParity.Odd );
 }  
  

But you're using decimals in your code, so there's a third possibility and you need to do 2 tests.

 EParity OddOrEven( float f )
 {
     if ( f % 2 == 0 ) 
         return EParity.Even;
     else if ( f % 2 == 1 ) 
         return EParity.Odd;
     return EParity.Neither;
 }
 

The bitwise version only works with integers so to use that with floats you need to check if it's an integer first..

 EParity OddOrEven2( float f )
 {
     int i = Mathf.FloorToInt( f );
     if ( f != float( i ) )
         return EParity.Neither;
     return OddOrEven2( i );
 }
 

...the first part of which is a way of doing what the question in the title asks for. Whether or not a float has anything after the decimal point is the same as asking if it's (not) an integer, and you can do that by casting back and forth then comparing.

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Casting-related error: "^ cannot be applied to double and int" 1 Answer

how to Break a vector3 line into half and Quater 2 Answers

How to move prefabs array in sine wave? 2 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