-1

My question is how do I convert from kg to pound and oz?

I know that 1kg=1000gm and 2 lb 3.274 oz (1 lb = 16 oz)

I will read a file containing:

weight 3000 gm for A and for B the weight is 90 kg

So the result for 3000 kg will be weight 188 lb and 1.6 oz.

static void ToLB(double Weight, string type)
{
    double Weightgram, kgtopounds;
    // lbs / 2.2 = kilograms
    // kg x  2.2 = pounds

    if (type == "g")
    {
        // convert gram to kg
        Weightgram = Weight * 1000;
        // then convert kg to lb
        kgtopounds = 2.204627 * Weight;
        //convert gram to oz" 

        Weightgram = Weightgram * 0.035274;
        Console.Write("\n");
        Console.Write(kgtopounds);
    }
// I want to convert each gram and kg to pounds and oz using C# 
CarenRose
  • 1,266
  • 1
  • 12
  • 24
sara white
  • 101
  • 1
  • 2
  • 6
  • 1
    Sorry I'm not sure exactly what you're asking here. Are you asking how to get ounces from a decimal value of pounds, or something else? – lc. Dec 28 '12 at 01:27
  • i want to convert kg to lb and oz for example 3000 g will be after conversion 188 lb and 1.6 ....we use RE in C# to get the weight in kg then convert it to lb and oz – sara white Dec 28 '12 at 01:36
  • 3
    I'm still confused - neither of 3000g, 300g, 300kg, or 3000kg will give you 188 lb and 1.6 oz. – lc. Dec 28 '12 at 01:41
  • 1
    Just base them all to something common and it's trivial, this isn't really a programming question? – Andrew Barrett Dec 28 '12 at 01:46
  • double WeightPounds = Weight/1000/2.204627 if weight is in grams, you will need to divide by 1000 to get the weight in kilograms, then you can divide by the 2.204627 to get the number of pounds. Any conversion for any unit of measure can be ready like the following: 1000g/1kg = 1000 grams per kilogram or 1kg/1000g = 1 kilogram per 1000 grams Finally, once you have your number (which is presumably 12.3456), take the numbers before the decimal point (number of pounds), as for number after the decimal point, this is a percentage of one, so just take it times 16 (number of ounces in a pound) – John Bartels Dec 28 '12 at 01:50

1 Answers1

2

You should instead use an enum for your type (that is, if it fits your model of reading the file and whatnot). Here's the solution I derived:

public static void ConvertToPounds(double weight, WeightType type)
{
    switch (type)
    {
        case WeightType.Kilograms:
        {
            double pounds = weight * 2.20462d;
            double ounces = pounds - Math.Floor(pounds);
            pounds -= ounces;
            ounces *= 16;
            Console.WriteLine("{0} lbs and {1} oz.", pounds, ounces);
            break;
        }
        default:
            throw new Exception("Weight type not supported");
    }
}

ideone link

Lander
  • 3,369
  • 2
  • 37
  • 53
  • Note: the used factor is very inaccurate for converting grams to pounds. Use `weight / 453.59237` instead. – maf-soft Mar 31 '17 at 09:52