ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What language are you programming in? In general the way that you do this is to read the input into a floating point variable. Then copy the contents of the floating point variable into an integer variable which will truncate the decimal portion of the number. Copy the integer back into a floating point number and compare the two floating point numbers. If they are equal then the original number was an integer.
For example read in 4.3. Copy it to an integer variable where it is truncated to 4. Copy the integer to a floating point variable where it becomes 4.0. Compare 4.0 to 4.3 and the answer is that 4.3 is not an integer.
i found this: http://python.active-venture.com/lib/module-string.html
This module defines some constants useful for checking character classes and some useful string functions. See the module re for string functions based on regular expressions.
That's what most Python programmers do. If you're adamant about not using exceptions, you could do (for non-negative values):
Code:
all(char in string.digits for char in value)
But "it's better to ask for forgiveness than permission."
i found this, it works and what was more what i was thinking of:
c=raw_input("yo: ")
if c in '0123456789':
print "integer"
else:
print "char"
however lets say the string is very large(WELL OBV NEVER IN THIS CASE BUT SAy i want see if it is "in" somethign else maybe its very expensive performance-wise?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.