-1

私の質問は、kgからポンドとオンスにどのように変換するのですか?

1kg=1000gmおよび2lb3.274 oz(1 lb = 16 oz)

以下を含むファイルを読み取ります。

AとBの重量は3000gmで、重量は90kgです。

したがって、3000 kgの結果は、重量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 に答える