PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 12, 2022

[FIXED] How to evaluate a string input and check if it is a decimal

 August 12, 2022     decimal, evaluation, input, python, string     No comments   

Issue

I use python 3 and am trying to figure out how to evaluate a user input string and check for certain characters.

I know that this title is probably going to make it sound like I haven't heard of the .isdecimal() method, but in fact I am trying to allow for a user to input some number as a decimal, including the period, and this is what seems to make the evaluation hard.

For instance,

dec = input()

dec.isdecimal()

Returns False if I have the input 0.5 I could convert the input to a decimal perhaps, but if I don't want to limit the user's input to whole numbers, I'm not sure how else I can approach this evaluation.


Solution

IIUC, you just want to check if your string has a decimal in it. In that case, you could simply do the following.

inp_string = input()

def check_decimal(inp_string):
    return ('.' in inp_string)

check_decimal(inp_string)


Answered By - Sheldore
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,217,793

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing