c#度量转换类
本文关键字:转换 度量 | 更新日期: 2023-09-27 18:15:22
我一直在写一个用于厨房测量的类,现在它只有体积或液体测量,我这样称呼它:
ktchnmsrmnts.VolumeMeasurements(number), current_measurement, to_measurement);
,从那里,大多数人都很容易找出里面的代码,只是一些简单的数学。现在我的问题是,我需要添加一个选项,说'嘿,他有17.50杯,让它是1加仑,和1 1/2杯。
我正在考虑为'to_measurement'添加一个新案例它会检查这个数字是否大于最高测量值,比如19杯大于加仑,然后它把它变成1加仑,然后把剩余的量发送到直线上,但我从不在厨房里使用品脱,所以也许可以添加另一个选项来转换它。我不知道我被困住了,是的,我知道代码是基本的,但符合我的需求,无论如何,一半…
如果你需要我的完整代码,给你:
class KitchenMeasurements
{
public double VolumeMeasurements(double number, string current_measurement, string to_measurement)
{
double mL = 0;
double tsp= 0;
double tbsp = 0;
double fluid_ounces = 0;
double cup = 0;
double pint = 0;
double quart = 0;
double gallon = 0;
double ounce = 0;
double gram = 0;
double pound = 0;
double liter = 0;
switch (current_measurement)
{
case "mL":
mL = number;
tsp = mL * 0.202884;
tbsp = mL * 0.067628;
fluid_ounces = mL * 0.033814;
cup = mL * 0.00422675;
pint = mL * 0.00211338;
quart = mL * 0.00105669;
gallon = mL * 0.000264172;
liter = mL * 0.001;
break;
case "tsb":
tsp = number;
mL = tsp * 4.92892;
tbsp = tsp * 0.333333;
fluid_ounces = tsp * 0.166667;
cup = tsp * 0.0208333;
pint = tsp * 0.0104167;
quart = tsp * 0.00520833;
gallon = tsp * 0.00130208;
liter = tsp * 0.00492892;
break;
case "tbsp":
tbsp = number;
mL = tbsp * 14.7868;
tsp = tbsp * 3;
fluid_ounces = tbsp * 0.5;
cup = tbsp * 0.0625;
pint = tbsp * 0.03125;
quart = tbsp * 0.015625;
gallon = tbsp * 0.00390625;
liter = tbsp * 0.0147868;
break;
case "fluid ounce":
fluid_ounces = number;
mL = fluid_ounces * 29.5735;
tsp = fluid_ounces * 6;
tbsp = fluid_ounces * 2;
cup = fluid_ounces * 0.125;
pint = fluid_ounces * 0.0625;
quart = fluid_ounces * 0.03125;
gallon = fluid_ounces * 0.0078125;
liter = fluid_ounces * 0.0295735;
break;
case "cup":
cup = number;
mL = cup * 236.588;
tsp = cup * 48;
tbsp = cup * 16;
fluid_ounces = cup * 8;
pint = cup * 0.5;
quart = cup * 0.25;
gallon = cup * 0.0625;
liter = cup * 0.236588;
break;
case "pint":
pint = number;
mL = pint * 473.176;
tsp = pint * 96;
tbsp = pint * 32;
fluid_ounces = pint * 16;
cup = pint * 2;
quart = pint * 0.5;
gallon = pint * 0.125;
liter = pint * 0.473176;
break;
case "quart":
quart = number;
mL = quart * 946.353;
tsp = quart * 192;
tbsp = quart * 64;
fluid_ounces = quart * 32;
cup = quart * 4;
pint = quart * 2;
gallon = quart * 0.25;
liter = quart * 0.946353;
break;
case "gallon":
gallon = number;
mL = gallon * 3785.41;
tsp = gallon * 768;
tbsp = gallon * 256;
fluid_ounces = gallon * 128;
cup = gallon * 16;
pint = gallon * 8;
quart = gallon * 4;
liter = gallon * 3.78541;
break;
case "liter":
liter = number;
mL = liter * 1000;
tsp = liter * 202.884;
tbsp = liter * 67.628;
fluid_ounces = liter * 33.814;
cup = liter * 4.22675;
pint = liter * 2.11338;
quart = liter * 1.05669;
gallon = liter * 0.264172;
break;
}
switch (to_measurement)
{
case "mL":
return mL;
case "tsb":
return tsp;
case "tbsp":
return tbsp;
case "fluid ounce":
return fluid_ounces;
case "cup":
return cup;
case "pint":
return pint;
case "quart":
return quart;
case "gallon":
return gallon;
case "ounce":
return ounce;
case "gram":
return gram;
case "pound":
return pound;
case "liter":
return liter;
}
}
}
很难确切地说出你在要求什么,但我确实看到了这个:
现在我的问题是我需要添加一个选项,说'嘿,他有17.50杯,让它成为1加仑,和1 1/2杯。
您需要获取一组测量值,并通过它们循环重新计算余数。我强烈建议您将方法代码提取到类变量和辅助方法中。
下面是一些示例代码,应该可以帮助您。你需要填空。
public string ConvertToDescription(double amount, string originalMeasurement, params string[] toMeasurements)
{
StringBuilder sb = new StringBuilder();
double valueToConvert = amount;
string priorMeasurement = originalMeasurement;
double displayAmount;
for (int i = 0; i < toMeasurements.Count; i++)
{
if (i > 0)
sb.Append(" ");
double convertedAmount = VolumeMeasurements(valueToConvert, priorMeasurement, toMeasurement[i]);
// Check if last item so we don't trim wanted decimals.
if (i < toMeasurements.Count - 1)
displayAmount = Math.Floor(convertedAmount)
else
displayAmount = convertedAmount;
valueToConvert = convertedAmount - displayAmount;
priorMeasurement = toMeasurements[i];
// You will need to add logic here to display fractions if needed.
sb.AppendFormat("{0} {1}", displayAmount, toMeasurements[i]);
if (fixedAmount == 0)
break;
}
return sb.ToString();
}
请记住,这些都没有经过测试,我真的希望这是你想要的。这应该能让你走上正轨。您需要找到一种方法将最后一次转换中的小数转换为分数并实现它。这至少会让你得到"1加仑1.5杯"