Question

I need a suggestion in finding difference between two lbs(pound) values. I tried in 2 ways,

First way,

var pound1 = 151.10;
var pound2 = 142.19;
var finalPoundValue = (pound1 - pound2);

// result 8.91 -> 8 Pounds 91 Ounce 

Second way,

var value1 = 151.10;
var value2 = 142.19;
var poundDiff = (151 - 142);
var ounceDiff = (10 - 19);

// result 9 Pounds -9 Ounce

but, don't know which one is the right way to find difference between pound values. Or any other best method available to solve this problem..?

Was it helpful?

Solution

8.91 lb is not 8 lb 91 oz, not by a long shot. It's 8 lb and (0.91 * 16 = ~ 15) oz, since there are 16 ounces to a pound.

I would use your first variant to get the difference in pounds, then some adjustments to turn that into pounds and ounces, something like:

var pound1 = 151.10;
var pound2 = 142.19;
var poundDiff = (pound1 - pound2);

var finalPoundValue = (int) poundDiff;
var finalOunceValue = (int) ((poundDiff - finalPoundValue) * 16);

OTHER TIPS

1 pount = 16 ounces.

So, you have to separate the number into the folloiwng: Assuming 8.91lb

  1. Pounds = int(8.91) = 8lb

  2. Ounces = (8.91 - int (8.91) ) * 16 = 14.56

Do the same for both numbers, and then add pounds to pounds, ounces to ounces (or subtract), then do the carry overs back. if you have more than 16 ounces when you added, you increase 1 pound, and subtract 16 from the ounces.

This is a quick answer for you to look at.

If I were using weight a lot, I would create a class to represent weight. The class would store weight in a single unit of measure and have read-only properties for getting different units of measure.

class Weight
{
    private double ounces;
    private const double OUNCES_PER_POUND = 16.0;

    public double Pounds
    {
        get { return ounces / OUNCES_PER_POUND; }
    }

    public double Ounces
    {
        get { return ounces; }
    }

    public Weight(double pounds)
    {
        this.ounces = pounds * OUNCES_PER_POUND;
    }

    public Weight(int pounds, double ounces)
    {
        this.ounces = pounds * OUNCES_PER_POUND + ounces;
    }

    // An example operator, probably want to implement addition
    // and perhaps multiplication/division as well
    public static Weight operator -(Weight w1, Weight w2)
    {
        return new Weight((w1.ounces - w2.ounces) / OUNCES_PER_POUND);
    }

    public override string ToString()
    {
        return String.Format("{0} pounds, {1} ounces", (int)Pounds, Math.Round(Ounces % OUNCES_PER_POUND, 4));
    }
}

class Program
{
    public static void Main()
    {
        var w1 = new Weight(151.10);
        var w2 = new Weight(142.19);
        Console.WriteLine("w1: " + w1.ToString() );         // 151 pounds, 1.6 ounces
        Console.WriteLine("w2: " + w2.ToString() );         // 142 pounds, 3.04 ounces
        Console.WriteLine("diff: " + (w1 - w2).ToString()); //   8 pounds, 14.56 ounces
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top