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# 
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top