如何我从克转换成磅C#
问题描述:
我的问题是如何我从公斤转换为磅和盎司 我知道 1公斤= 1000克和2磅3.274盎司(1磅= 16盎司)如何我从克转换成磅C#
我将读取文件包含
重量3000克为A和B的重量是90公斤 SO为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 garm to oz"
Weightgram = Weightgram * 0.035274;
Console.Write("\n");
Console.Write(kgtopounds);
}
// i want to convert each gram and kg to bound an oz using c#
答
你应该使用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");
}
}
+0
注意:使用的因子是将克转换为磅非常不准确。改用'weight/453.59237'。 –
对不起,我不知道你问这里什么。你问的是如何从磅的十进制值或其他东西获得盎司? –
我想转换公斤到磅和盎司例如3000克将转换后188磅和1.6 ....我们在C#中使用RE来获得公斤的重量,然后将其转换为磅和盎司 –
我仍然困惑 - 3000克,300克,300公斤或3000公斤都不会给你188磅和1.6盎司。 –