0

I draft custom cabinetry, and sometimes a run will need to be divided into say 3 cabinets, and I cannot have each cabinet section equal because the section width has to be imperial (to the 1/32") and cannot have a third of an inch. I generally have to do some rough math and adjust the widths as needed to maintain the total length of the run and maintaining imperial measurement, but I am writing a python program to calculate this for me. I will admit I am a hack at Python, but I am doing my best. This is what I have so far - how can 1) round each of the section widths to the nearest 1/32" while 2) maintaining the overall width? (i.e., not rounding all to the nearest because it may throw off the overall width) Thank you!

import math
# input total length of run
run_length = float(input("Enter length of the run, in inches:"))
# accounting for 1/8" reveals
reveal_num = float(input("Enter number of reveals: "))
reveal_length = reveal_num * 0.125
true_length = run_length - reveal_length
# determining number of run sections
section_num = float(input("Enter desired number of doors/sections: "))
# This is where the fun begins
# First group is nicely divisible into whole numbers
if true_length % section_num == 0:
    section_length = true_length / section_num
    print("Each section will be", section_length, "inches")
# Second group is divisible by 32nds, tested by multiplying by 32 and seeing if result is a 
whole number
# Then to be rounded to the nearest 1/32" while maintaining the total width
elif ((true_length / section_num) * 32).is_integer():
# Third group is the misfits. Must be divided to the nearest 1/32" while maintaining the total 
width.
else:
  • You might get some inspiration from [How to make rounded percentages add up to 100%](https://stackoverflow.com/q/13483430/5987). – Mark Ransom Jul 22 '21 at 15:58

1 Answers1

0

So there's one way to do this, but it's fairly involved. First, you'll need to convert your width to 32nd's, which you can do by multiplying by 32. Then, to avoid going over you want to take the floor of the remaining floating point operation. Luckily, this is the default behavior when converting to integers, so you could do something like this to get your conversion:

width32 = width * 32
width32_floor = int(width32)
new_width = float(width32_floor) / 32

The float is needed so that you have a genuine width and it doesn't take the floor after the division. Once you have converted all your widths this way, you can take the difference between the total width and the sum of your individual widths and distribute them evenly (or however you like) among the individual widths.

Guy Marino
  • 429
  • 3
  • 6
  • The `float(...)` is not needed in Python 3. Since Python 2 is no longer supported, you can assume questions are about Python 3 unless otherwise stated. – kaya3 Jul 22 '21 at 16:11