Converting ounces to pounds and ounces acurately

hi all

Im writing some code at the moment where i log my fish catches when i go fishing.
My problem is i want to calculate the total weight at the end of the season, I have a columns for pounds, ounces and totalounces (total ounces is just pounds divided by 16 plus the ounces).

I thought i could just take the total ounces and divide by 16 which is what i tried but that doesnt come out correct.
an example would be 18 lb 11 ounces = 299 totalounces divide that by 16 and you get 18.69 (which isnt 18 lb 11

Maths is not my best subject so im confused if you need any more information or theres an easier way please let me know.

Thank you in advance
Dave

The .69 is the fractional part of a pound. To get how many ounces this is, you multiple it by 16.

In programming, there’s usually a Modulus (MOD) function you can use to get the remainder of a division, so something like MOD(299,16) will give you the remainder in ounces.

Here’s an SQL example showing how you can get the pounds and ounces from a value - SELECT FLOOR(299/16) as lb, MOD(299,16) as oz;

Your database table should only store the raw data, not derived data. If you are recording pounds and ounces, that is the only weight you should store. You should only covert to total ounces when you need to and you can do this directly in the query being used to produce the total pounds and ounces value.

Hi thanks for the answer,
reading it makes sense in my head but im afraid as im a noob im still unsure how to use that call so it actually works.

Sorry im a noob and i havent learnt this much yet.

Thank you for your understanding

Dave

phdr is saying you only need 1 column in your table.
I think just ounces would be best:
When you weigh your fish your scale probably tells you lbs and oz?
Multiply lbs by 16, add on oz and store just that number in your table as ‘weight’.
Then sql for a fish is just “select weight from table” and in your subsequent code something like

$pounds = round($weight/16,0);
$ounces = $weight/16 - $pounds;
echo “weight = “.$pounds.” lbs “.$ounces.” oz.”;

Your total ounces is just sum(weight) - it’s already oz.

thank you,

i understand now as i say it made sense in my head but when you haven’t done it before its difficult to write it correctly.

Thank you.
Dave

Sponsor our Newsletter | Privacy Policy | Terms of Service