Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python won 't do decimals
#1
It took me a lot of tries before I was able to figure out the problem, and I was stunned. When building my genealogy website, I display 100 entries per page, but I don't want final pages with only a small handful. If there remain 101 entries I'd rather display 51 on one page and 50 on the next. It should have been simple with

z = (remainder / 2)
z = int(z + 0.6)
However, it makes things worse. It takes the first line and instead of z = 50.5 it says z = 50 and the next line doesn't help. The program puts 50 lines on one page, 50 on the next and then builds a page with one lone entry. I had to work around and say

if remainder % 2 == 1:
     z = z + 1
I don't recall this happening before in the years I've been coding in Python.

What is the way to say I absolutely want a decimal answer to remainder / 2 and also not a string variable because I need to do more math with it ?
Reply
#2
In Python 3, 101 / 2 returns 50.5. Are your running Python 2? That does integer division if both numbers are ints. If that is the case, divide by 2.0 and you will get a float. But why would you be using Python 2?

If it is not Python 2, maybe this is not Python, but some python-like thing. You mention this is for a website. What tools are you using for the website?
Reply
#3
It looks like you are using python2

Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 5/2
2.5
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I'm running Python 3 using Terminal on my Mac. I type in

python example.py at the command prompt.
Reply
#5
Please show the output of python -V and the contents of example.py. I cant run your first example because remainder isn't assigned.
Reply
#6
(Jun-08-2023, 03:25 PM)SomebodySmart Wrote: I'm running Python 3 using Terminal on my Mac.

I doubt it. Check https://peps.python.org/pep-0238/
and
https://docs.python.org/3/using/mac.html#

Probably you have python2 preinstalled and python refers to that one.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 1,298 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  Adding Decimals to classes with OOP + rounding to significant digits (ATM demo) Drone4four 7 2,326 May-04-2022, 06:15 AM
Last Post: Drone4four
  floats 2 decimals rwahdan 3 1,644 Dec-19-2021, 10:30 PM
Last Post: snippsat
  Converting decimals stylingpat 3 2,138 Mar-27-2021, 02:32 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020