How to make python print 1 as opposed to 1.0

Question:

I am making a math solving program, it keeps printing the whole numbers as decimals. Like 1 is 1.0, 5 is 5.0, and my code is:

print("Type in the cooridinates of the two points.")
    print("")
    print("---------------------")
    print("First point:")
    x1 = int(input("X: "))
    y1 = int(input("Y: "))
    print("")
    print("---------------------")
    print("Second point:")
    x2 = int(input("X: "))
    y2 = int(input("Y: "))
    m = (y1-y2) / (x1-x2)
    b = y1 - m * x1
    round(m, 0)
    round(b, 0)
    print("Completed equation:")
    print("")
    if b < 0:
        print("Y = "+ str(m) +"X - "+ str(b) +".")
    elif b > 0:
        print("Y = "+ str(m) +"X + "+ str(b) +".")
    elif b == 0:
        print("Y = "+ str(m) +"X.")
    input("Press enter to continue.")
Asked By: Charlie Kimzey

||

Answers:

Since you’re dividing integers, Python represents the result as a float, not as an int. To format the floats as you’d like, you’ll have to use string formatting:

>>> print('{:g}'.format(3.14))
3.14
>>> print('{:g}'.format(3.0))
3

So plugging it into your code:

print("Y = {:g}X - {}.".format(m, b))
Answered By: Blender

Try this,

> x = 10.0
> print int(x)
10
> print x
> 10.0

Is this what you are really looking for?

Answered By: kumar_m_kiran

When you do this:

m = (y1-y2) / (x1-x2)

You get a float (a floating point representation of a real number), not an int (an integer).

Then, because m is a float, b too is a float. Then, when you call str on a float, you get a decimal point.

The right way to fix this depends on what you actually want to do.

If you really want to deal only with integers, you can use integer division:

m = (y1-y2) // (x1-x2)

That means if, say, x1, x2, y1, y2 = 4, 2, 2, 1 you’ll end up with b = 2 (2 - 0 * 4). I suspect that isn’t what you want; you actually want to multiply that 4 by 1/2, and then round the result afterward.

In that case, you can do the conversion the same place you round:

m = int(round(m, 0))
b = int(round(b, 0))

Note that your existing calls to round didn’t actually do anything; round(m, 0) doesn’t modify m, it returns a new value that’s the rounded version of m, which you have to assign back to m (or somewhere else).

One more thing to consider: You probably really do want literally one half, not “the closest floating point representation to 0.5”.

You probably don’t actually care about the difference—unless you’re using quite large integers, the rounding errors won’t ever be visible. But if you do care, you may want to consider using a third-party exact-fraction module (e.g., clnum), or possibly the standard library decimal. This would mean you have to be more explicit, so I wouldn’t do this unless you expect it to be necessary. But if it is necessary, it would look something like this:

m = decimal.Decimal(y1-y2) / (x1-x2)
b = y1 - m * x1
m = int(m.to_integral(rounding=decimal.ROUND_HALF_UP))
b = int(b.to_integral(rounding=decimal.ROUND_HALF_UP))

Finally, you can always keep the numbers around as floats (or Decimals, etc.), never rounding or converting them, and force them to print as integers anyway:

print("Y = {:.0g}X - {:.0g}.".format(m, b))
Answered By: abarnert
Just tried this one

n = -0.0
#n = 0.0
#n = +0.0
#n = 5.0
#n = 5
#n = 5.4
#n = -5.0
#n = -5
#n = 5.25

n = (n**2)**0.5

n = '{:g}'.format(n)

if n.find('.') == -1:
    n=int(n)
else:
    n=float(n)

print(n)
print(type(n))


-------------------------------------------------------------------

The same in one line

n = -0.0
n = int('{:g}'.format((n**2)**0.5)) if 
 '{:g}'.format((n**2)**0.5).find('.') == -1 else 
float('{:g}'.format((n**2)**0.5)) 


-------------------------------------------------------------------

Example for a list


numlist = [-0.0, 0.0, +0.0, 5.0, 5, 5.4, +5.0, +5, +5.15,-5.0, -5, 5.25]
print(f'Numbers are {numlist}')

for n in numlist:
    print(f'Before n is {n} and type of n is {type(n)}')
    n = int('{:g}'.format((n**2)**0.5)) if '{:g}'.format((n**2)**0.5).find('.') 
    == -1 else float('{:g}'.format((n**2)**0.5)) 
    print(f'After n is {n} and type of n is {type(n)}')
Answered By: Pradeep VN
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.