Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Python Rounding problems


ghxst's Avatar
Member
0 0

HI guys,

I decided to write a calculator for Warehouselogistics and practice!

I set up a few vars and everything is working well so far. My only problem is, when it calculates 542 / 32 I get 16. It is 16,94 and should round up to 17.

I googled rounding in python and used int(round(x, 0)). ( x would be 16,94? ). i also tried just round(x) or round(x, 0), i thought for an half hour, but i just can't figure my fault out so i decided to post my problem here.

Edit: I wrote Import math on the top just to be sure If my full code is needed, let me know!

Edit: Could it be because I used variables for the calculation and not any number? I'm pretty new to Python so I don't know the most commands like "def" ^^

Thanks in Advance


techb's Avatar
Member
0 0

Python doesn't like decimals. Try using the math lib, or other built in libs for decimals and the likes. Trust me, you just need to look harder.


ghxst's Avatar
Member
0 0

PROBLEM: I set the numbers and vars to Int, Integer is for round numbers and not any decimals.

right?

b = 28 [ in this case, in my code it is raw_input('length? ') ]

Edit: L = 120 / b

l = 120 / b TypeError: unsupported operand type(s) for /: 'int' and 'str'

Solution: No clue, not yet!


ghost's Avatar
0 0

ghxst wrote: PROBLEM: I set the numbers and vars to Int Why not try setting them to float? Just a guess.


ghost's Avatar
0 0

ghxst wrote: PROBLEM: I set the numbers and vars to Int, Integer is for round numbers and not any decimals.

right?

b = 28 [ in this case, in my code it is raw_input('length? ') ]

Edit: L = 120 / b

l = 120 / b TypeError: unsupported operand type(s) for /: 'int' and 'str'

Solution: No clue, not yet!

b=float((raw_input("numbah: ")) L=120./b the decimal point after the 120 will make it a float. either way will do. only one of them needs to be for it to be evaluated as a float. int() will interpret a string as an integer


ghxst's Avatar
Member
0 0

It seems that Python thinks that 542/32 is 16. I typed just 542/32 into the shell and it output me 16.

I also typed float(542/32) output -> 16.0

round(542/32) -> 16.0

Did i do something wrong?

Again I'm pretty new to Python, just trying every command until I find the one that works ^^


ghost's Avatar
0 0

ghxst wrote: I also typed float(542/32) output -> 16.0

Well, if you make an int (something that is only 16) into a float then obviously it won't go much higher than 16.0. Float is to be decided before division.


ghxst's Avatar
Member
0 0

I decided to post my code, I can't figure it out myself :|

Task: 542cartons, length 28, broad 37, height 48, 20kg per carton and 230 cm height of the truck (all in cm)

original code

a = raw_input('How many cartons? ')
b=raw_input('Length? ')
c=raw_input('Broad? ')
d=raw_input('Height? ')
e=raw_input('Kg per carton? ')
f=raw_input('truck height? ')

l = int(120) / int(b)
ba = int(80) / int(c)
h = int(f) / int(d)

print l

print ba

print h

y = l*ba*h

print y

p = int(a) / int(y)

print p

raw_input('finished for now!')```


p outputs me 16

note: p should always be rounded up, i just remembered ^^

tkearn5000's Avatar
Member
0 0

Look up the concept of integer division. That is what is happening to you. In many languages when you divide an integer by another int the answer is truncated to an integer. In the process of truncation the digits after the decimal are dropped completely, not rounded. You need to convert at least one of the ints to float.

Ex. In the shell entering 9/5 = 1. But entering 9/5.0 or 9.0 /5 = 1.8.

Also, this can depend on the version of Python that you have. Starting with version 3.1 and later, division using / will always give you the correct answer (9/5 = 1.8), and integer division is written as 9//5 = 1.

Hope this helps.


ghxst's Avatar
Member
0 0

nope sorry, when I type 9 / 5 into the shell, I get 1. When I type 9 // 5 I get 1 too :s, someone told me // is to extract a root? ah I really don't know, but i don't get a decimal number on both cases


tkearn5000's Avatar
Member
0 0

9/5 will only give you a decimal in Python 3.1 or later. You need to be using floats instead of ints. Type 9.0/5 or 9/5.0 and see what happens. And read about integer division.


ghxst's Avatar
Member
0 0

Thank you so much tkearn5000

also thanks COM, told me right, I just didn't know how to do it right. I used float a bit wrong :s

tkearn5000 also pointed me into the right direction with round() =)

finally I can finish it!


stealth-'s Avatar
Ninja Extreme
0 0

Wow, I can't believe it took that long for him to get a straightforward answer, guys.

@OP This was my first major issue with programming. When I first started with python (quite a while back), I pretty much almost gave up because I couldn't manage to get a simple interest calculator to work. I think whoever is writing python tutorials should make that stuff more clear for beginners >.>


techb's Avatar
Member
0 0

also:

>>>542/32
16
>>>
>>>542.0/32.0
16.9375```

So:
```markup
float(542/32)

should be:

float(542)/float(32)

Also its a good idea to include a rounding digit; not needed, but good practice. example:

>>> round(542.0/32.0,0)
17.0
>>>

from there, you can convert to int or whatever.


ghost's Avatar
0 0

You'd have less trouble, regardless of how new you are to a language, if you read the fucking replies.