Store the value in a known unit of measurement internally (e.g. store everything in kg) and have equations to convert to/from the known unit to all the others.
Basically you only need to convert the units for display, all your internal calculations can be done in a single unit.
As a curve ball, F# has a robust UoM mechanism inbuilt if you fancy something cutting edge to play with...
namespace Units
{
class Weight
{
public enum WeightType
{
kg,
lb
}
const double kgTolbs = 2.20462262;
public static double Convert(double value, WeightType fromUnits, WeightType toUnits)
{
//Code here to convert units
}
}
}
通过这种结构,您可以随意在任何单位类型之间进行转换。
In a program that I've worked on we had separate classes under a "Units" namespace, such as Units.Torque or Units.Length.
If you create a class like Units.Weight and have all the conversion factors as const values and the possible units as an enum, you can just call that class and convert to the desired units. This is a very basic example of the system we use:
namespace Units
{
class Weight
{
public enum WeightType
{
kg,
lb
}
const double kgTolbs = 2.20462262;
public static double Convert(double value, WeightType fromUnits, WeightType toUnits)
{
//Code here to convert units
}
}
}
With this structure you can convert to and from any of the unit types at your leisure.
//As a stateless variable
UnitOf.Mass unit = new UnitOf.Mass() .FromPounds(20);
double kilograms = unit.ToKilograms(); //9.07184
double ounces = unit.ToOuncesUS(); //319.99973897268154
//As one liners
double kilogram = new UnitOf.Mass().FromPounds(20).ToKilograms(); //9.07184
double ounces = new UnitOf.Mass().FromPounds(20).ToOuncesUS(); //319.99973897268154
质量只是 UnitOf 中可用的 20 个测量值之一,而且也是免费的。开源。
We just officially released our lightweight measurement and data type conversion library, UnitOf, for C# (also for Java & JavaScript) that can do just what you are asking without needing to know any conversion factors. You can declare a stateless UnitOf.Mass variable allowing you to convert to any unit or just perform the conversions as one liners:
//As a stateless variable
UnitOf.Mass unit = new UnitOf.Mass() .FromPounds(20);
double kilograms = unit.ToKilograms(); //9.07184
double ounces = unit.ToOuncesUS(); //319.99973897268154
//As one liners
double kilogram = new UnitOf.Mass().FromPounds(20).ToKilograms(); //9.07184
double ounces = new UnitOf.Mass().FromPounds(20).ToOuncesUS(); //319.99973897268154
Mass is just 1 of the 20 measurements available in UnitOf which is also free & open-source.
发布评论
评论(4)
使用标准单位(或您喜欢使用的单位)进行所有计算。
只有用户界面需要关心单位转换。其余的代码不需要担心它。
Do all your calculations in standard units (or the units you prefer to work with).
Only the user interface needs to care about the unit conversions. The rest of the code does not need to worry about it.
将值存储在内部已知的测量单位中(例如,将所有内容存储为千克),并使用方程式将已知单位转换为所有其他单位或从已知单位转换为所有其他单位。
基本上你只需要转换显示单位,所有内部计算都可以用一个单位完成。
作为一个曲线球,如果您喜欢玩一些尖端的东西,F# 内置了强大的 UoM 机制......
Store the value in a known unit of measurement internally (e.g. store everything in kg) and have equations to convert to/from the known unit to all the others.
Basically you only need to convert the units for display, all your internal calculations can be done in a single unit.
As a curve ball, F# has a robust UoM mechanism inbuilt if you fancy something cutting edge to play with...
在我编写的一个程序中,我们在“Units”命名空间下有单独的类,例如Units.Torque 或Units.Length。
如果您创建像 Units.Weight 这样的类,并将所有转换系数作为常量值,将可能的单位作为枚举,则只需调用该类并转换为所需的单位即可。这是我们使用的系统的一个非常基本的示例:
通过这种结构,您可以随意在任何单位类型之间进行转换。
In a program that I've worked on we had separate classes under a "Units" namespace, such as Units.Torque or Units.Length.
If you create a class like Units.Weight and have all the conversion factors as const values and the possible units as an enum, you can just call that class and convert to the desired units. This is a very basic example of the system we use:
With this structure you can convert to and from any of the unit types at your leisure.
我们刚刚正式发布了适用于 C#(也适用于 Java)的轻量级测量和数据类型转换库 UnitOf和 JavaScript),它可以满足您的要求,而无需知道任何转换系数。您可以声明一个无状态的 UnitOf.Mass 变量,允许您转换为任何单位或仅将转换作为一个衬垫执行:
质量只是 UnitOf 中可用的 20 个测量值之一,而且也是免费的。开源。
We just officially released our lightweight measurement and data type conversion library, UnitOf, for C# (also for Java & JavaScript) that can do just what you are asking without needing to know any conversion factors. You can declare a stateless UnitOf.Mass variable allowing you to convert to any unit or just perform the conversions as one liners:
Mass is just 1 of the 20 measurements available in UnitOf which is also free & open-source.