2

I am working on an OSCommerce site, and for the USPS Shipping method I want to convert the weight unit from Pounds to Ounces, but not getting the way how.

Can anyone help?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Dheeraj Agrawal
  • 2,347
  • 11
  • 46
  • 63

1 Answers1

4

Given that 1 pound = 16 ounces, you can basically just multiply it by 16:

$pounds = 8;
$ounces = $pounds * 16;
echo $ounces; // 128

...but, if pounds is a float, you will probably want to round it. Since you are talking about shipping weights, you probably want to round up:

$pounds = 8.536;
$ounces = ceil($pounds * 16);
echo $ounces; // 137

You could put this into a function, like this:

function pounds_to_ounces ($pounds) {
  return ceil($pounds * 16);
}

echo pounds_to_ounces(8); // 128
echo pounds_to_ounces(8.536); // 137
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • that's great, thank you so much... it worked... still I have one more problem.. now how could I set rate from USPS? – Dheeraj Agrawal Dec 10 '11 at 12:43
  • You need to use the USPS rating apis. https://www.usps.com/business/webtools.htm or http://www.marksanborn.net/php/calculating-usps-shipping-rates-with-php/ – andyknas Jan 05 '12 at 17:44