0

I need to check if a number is a whole or a decimal and input it into an if statement ex.

if string_name.isdecimal() = true:
    print('warning')

I'm trying to check for any overflow.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • First, `true` => `True`. Second, you should use `try/except`. If not, see a link to the dupe. – DYZ Jan 12 '22 at 01:21
  • "if a number is a whole or a decimal" - *what does this mean?* What is your input - is it a `float` where you want to check if it's equal to an integer value? Or is it a string where you want to decide whether to convert it to `float` or to `int`? "I'm trying to check for any overflow." - what sort of overflow are you worried about? Why do you think there is such a possibility? – Karl Knechtel Jan 12 '22 at 01:22
  • @KarlKnechtel `isdecimal()` is a standard method on Python strings, documented to "Return True if all characters in the string are decimal characters and there is at least one character, False otherwise." – Grismar Jan 12 '22 at 01:24
  • Welcome to Stack Overflow. Please read [ask] and https://stackoverflow.com/help/minimal-reproducible-example. Show *complete* examples - that means: 1) show how `string_name` gets its value (by just assigning it; we don't care about whatever files you're reading etc.); 2) show *exactly* what should happen for that value, and explain why. Give enough examples to make it clear what you are trying to do, and *why* you are trying to do it. For example, if you are worried about "overflow", show examples that might cause such an overflow. – Karl Knechtel Jan 12 '22 at 01:24
  • @Grismar I'm aware, but that doesn't make the *intent of the question* clear. If `.isdecimal` is sufficient to solve the problem, then there wouldn't be any question to ask, right? – Karl Knechtel Jan 12 '22 at 01:25
  • @andrewharrison what exactly do you need to happen if `string_name` is decimal? There will only be "overflow" if it ends up being a representation of floating point value - Python `int` does not have a limit to size. – Grismar Jan 12 '22 at 01:25
  • 1
    @DYZ I'm not sure that duplicate actually answers OP's question (which is not at all clear). The method `.isdecimal` checks for a value that is an integer, but OP seems to be using the word "decimal" (quite reasonably, for someone who has heard of a "decimal point") to mean a non-integer, real number. – Karl Knechtel Jan 12 '22 at 01:28
  • Your ruby is showing – dawg Jan 12 '22 at 01:32
  • The equality operator is `==`, not `=`. But in a condition you don't need to test `== True`. It's redundant. You can write directly `if string_name.isdecimal():` – Stef Jan 12 '22 at 10:30

0 Answers0