This function I did to improve calculator program by eliminating problems with Pythons problem of dividing integer with integer and giving integer division result.
1/3=0 eliminator
def endswithint(s):
return not s.rstrip('0123456789').endswith('.')
def addpoint(s):
""" returns '.' if s ends with integer, nothing if it endswith float """
if endswithint(s): return '.'
return ""
a="2+1"
b="3"
print eval(a+'/'+b) # integer division
print eval('1.0*'+a+'/'+b) ##also does not make it
a+=addpoint(a)
print eval(a+'/'+b)
jcao219 18 Posting Pro in Training
snippsat 661 Master Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
snippsat 661 Master Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.