UnitConversion - .Net Core和.Net框架的可扩展的单位转换库

网友投稿 569 2022-10-31

UnitConversion - .Net Core和.Net框架的可扩展的单位转换库

UnitConversion -  .Net Core和.Net框架的可扩展的单位转换库

Unit Conversion

What is it?

An expansible .Net class library with support for all modern platforms

.Net Framework 4.0+.Net Standard 1.3+ (.Net 4.5.1+ and .Net Core)

UnitConversion is designed to be expansible through factories or through concrete converter implementations.

Implementations are currently limited, but include:

MassConverterDistanceConverterTimeConverterAreaConverterVolumeConverter

Pull requests with custom implementations are welcome! Please have a look through the contributing guidelines and our code of conduct.

Example

double kgValue;double lbValue;// Simple programmatic approach to conversion, using string `Synonyms`var kgToLbs = new MassConverter("kg", "lbs");kgValue = 452;lbValue = kgToLbs.LeftToRight(kgValue);Console.WriteLine("452kg in pounds is " + lbValue);// Converting both ways is easykgValue = kgToLbs.RightToLeft(lbValue);Console.WriteLine("Converted back: " + kgValue);// Rounding is part of conversionlbValue = kgToLbs.LeftToRight(kgValue, 2);Console.WriteLine("452kg in pounds (to 2 decimal places) is " + lbValue);// You can easily customise converters to support Synonyms used in// business logic, such as those stored on a databasekgToLbs.AddSynonym("kg", "MTOW (KG)");kgValue = 3000;kgToLbs.UnitLeft = "MTOW (KG)";lbValue = kgToLbs.LeftToRight(kgValue);Console.WriteLine("3000 MTOW (KG) in lbs is " + lbValue);// Add a new unit with a custom conversion factorkgToLbs.AddUnit("Chuck Norris", 9001);kgToLbs.UnitRight = "Chuck Norris";kgValue = 7;var chucks = kgToLbs.LeftToRight(kgValue);Console.WriteLine("7kg is equal to " + lbValue + " chucks");

Converters are easy to define and contribute to:

public class DistanceConverter : BaseUnitConverter { UnitFactors units = new UnitFactors("m") { { new UnitFactorSynonyms("m", "metre"), 1 }, { new UnitFactorSynonyms("km", "kilometre"), 0.001 }, { new UnitFactorSynonyms("cm", "centimetre"), 100 }, { new UnitFactorSynonyms("mm", "millimetre"), 1000 }, { new UnitFactorSynonyms("ft", "foot", "feet"), 1250d / 381 }, { new UnitFactorSynonyms("yd", "yard"), 1250d / 1143 }, { "mile", 125d / 201168 }, // strings automatically cast to UnitFactorSynonyms { new UnitFactorSynonyms("in", "inch"), 5000d / 127 }, }; public DistanceConverter(string leftUnit, string rightUnit) { Instantiate(units, leftUnit, rightUnit); } public DistanceConverter() { Instantiate(units); }}

Current Maintainer

@atulmish

Previous maintainers

Many thanks to the original maintainers @Nick-Lucas and @gkampolis for all their hard work.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于@Autowired注解和静态方法及new的关系
下一篇:使用PHP框架Phalcon开发一个简易的博客系统
相关文章

 发表评论

暂时没有评论,来抢沙发吧~