-1

我的问题是如何从公斤转换为磅和盎司?

我知道 1kg=1000gm 和 2 lb 3.274 oz (1 lb = 16 oz)

我将阅读包含以下内容的文件:

A 重量 3000 gm,B 重量 90 kg

所以 3000 公斤的结果将是188 磅和 1.6 盎司

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# 
4

1 回答 1

2

您应该改为使用enum您的类型(也就是说,如果它适合您的文件读取模型等等)。这是我得出的解决方案:

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链接

于 2012-12-28T01:54:00.420 回答