.NET是否具有單位轉換類?我需要將英寸轉換爲毫米,反之亦然。.NET單位類,英寸到毫米
回答
不,沒有像內置的東西。但你可以簡單地乘以或除以25.4。
不,沒有這樣的單位轉換內置到框架。雖然應該很容易實現自己。
不,你需要自己做一個,這樣的:
public class Length
{
private const double MillimetersPerInch = 25.4;
private double _Millimeters;
public static Length FromMillimeters(double mm)
{
return new Length { _Millimeters = mm };
}
public static Length FromInch(double inch)
{
return new Length { _Millimeters = inch * MillimetersPerInch };
}
public double Inch { get { return _Millimeters/MillimetersPerInch; } }
public double Millimeters { get { return _Millimeters; } }
}
.NET框架沒有這樣的事,但F#不Units of Measure。
Csunits是C#的一個很好的測量單位庫,見https://github.com/cureos/csunits。它目前適用於放射治療,但您可以輕鬆添加自己的單位和數量。
我以前處理過這個問題。我建議爲距離做兩個類。一種具有帝國措施,另一種具有度量措施。然後,您可以輕鬆地在它們之間來回轉換,但有一個明顯的警告,那就是您在執行時失去了精度。
下面是以英寸爲度量單位的英制距離等級的示例。
public class ImperialDistance {
public static readonly ImperialDistance Inch = new ImperialDistance(1.0);
public static readonly ImperialDistance Foot = new ImperialDistance(12.0);
public static readonly ImperialDistance Yard = new ImperialDistance(36.0);
public static readonly ImperialDistance Mile = new ImperialDistance(63360.0);
private double _inches;
public ImperialDistance(double inches) {
_inches = inches;
}
public double ToInches() {
return _inches;
}
public double ToFeet() {
return _inches/Foot._inches;
}
public double ToYards() {
return _inches/Yard._inches;
}
public double ToMiles() {
return _inches/Mile._inches;
}
public MetricDistance ToMetricDistance() {
return new MetricDistance(_inches * 0.0254);
}
public override int GetHashCode() {
return _inches.GetHashCode();
}
public override bool Equals(object obj) {
var o = obj as ImperialDistance;
if (o == null) return false;
return _inches.Equals(o._inches);
}
public static bool operator ==(ImperialDistance a, ImperialDistance b) {
// If both are null, or both are same instance, return true
if (ReferenceEquals(a, b)) return true;
// if either one or the other are null, return false
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false;
// compare
return a._inches == b._inches;
}
public static bool operator !=(ImperialDistance a, ImperialDistance b) {
return !(a == b);
}
public static ImperialDistance operator +(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches + b._inches);
}
public static ImperialDistance operator -(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches - b._inches);
}
public static ImperialDistance operator *(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches * b._inches);
}
public static ImperialDistance operator /(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches/b._inches);
}
}
而這裏的度量類如米,基本單元:
public class MetricDistance {
public static readonly MetricDistance Milimeter = new MetricDistance(0.001);
public static readonly MetricDistance Centimeter = new MetricDistance(0.01);
public static readonly MetricDistance Decimeter = new MetricDistance(0.1);
public static readonly MetricDistance Meter = new MetricDistance(1.0);
public static readonly MetricDistance Decameter = new MetricDistance(10.0);
public static readonly MetricDistance Hectometer = new MetricDistance(100.0);
public static readonly MetricDistance Kilometer = new MetricDistance(1000.0);
private double _meters;
public MetricDistance(double meters) {
_meters = meters;
}
public double ToMilimeters() {
return _meters/Milimeter._meters;
}
public double ToCentimeters() {
return _meters/Centimeter._meters;
}
public double ToDecimeters() {
return _meters/Decimeter._meters;
}
public double ToMeters() {
return _meters;
}
public double ToDecameters() {
return _meters/Decameter._meters;
}
public double ToHectometers() {
return _meters/Hectometer._meters;
}
public double ToKilometers() {
return _meters/Kilometer._meters;
}
public ImperialDistance ToImperialDistance() {
return new ImperialDistance(_meters * 39.3701);
}
public override int GetHashCode() {
return _meters.GetHashCode();
}
public override bool Equals(object obj) {
var o = obj as MetricDistance;
if (o == null) return false;
return _meters.Equals(o._meters);
}
public static bool operator ==(MetricDistance a, MetricDistance b) {
// If both are null, or both are same instance, return true
if (ReferenceEquals(a, b)) return true;
// if either one or the other are null, return false
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false;
return a._meters == b._meters;
}
public static bool operator !=(MetricDistance a, MetricDistance b) {
return !(a == b);
}
public static MetricDistance operator +(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters + b._meters);
}
public static MetricDistance operator -(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters - b._meters);
}
public static MetricDistance operator *(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters * b._meters);
}
public static MetricDistance operator /(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters/b._meters);
}
}
及這裏的例證使用的測試方法。
[TestMethod]
public void _5in_Equals_12_7cm() {
var inches = new ImperialDistance(5);
var cms = new MetricDistance(MetricDistance.Centimeter.ToMeters() * 12.7);
var calcCentimeters = Math.Round(inches.ToMetricDistance().ToCentimeters(), 2, MidpointRounding.AwayFromZero);
var calcInches = Math.Round(cms.ToImperialDistance().ToInches(), 2, MidpointRounding.AwayFromZero);
Assert.AreEqual(cms.ToCentimeters(), 12.7);
Assert.AreEqual(calcCentimeters, 12.7);
Assert.AreEqual(inches.ToInches(), 5);
Assert.AreEqual(calcInches, 5);
}
您還可以添加擴展方法
public static MetricDistance Centimeters(this Int32 that) {
return new MetricDistance(MetricDistance.Centimeter.ToMeters() * that);
}
[TestMethod]
public void _100cm_plus_300cm_equals_400cm() {
Assert.AreEqual(100.Centimeters() + 300.Centimeters(), 400.Centimeters());
}
您可以使用重量,溫度,液體措施,這個簡單的策略,等
'MetricDistance'和'ImperialDistance'都應該是結構。 – Georg 2017-06-14 06:50:59
這是一個選項,當然。 – mattmc3 2017-06-14 13:56:07
[UnitsNet](https://nuget.org/packages/UnitsNet)nuget採取了類似的方法,但涵蓋了大量的數量和單位。 – angularsen 2017-09-30 17:11:42
- 1. 的Excel英尺和英寸到毫米
- 2. PHP毫米(英寸)到英寸(英寸)和千克(磅)英鎊(磅)轉換
- 3. CSS英寸/毫米測量不準確
- 4. 我嘗試程序釐米英寸/英尺 - 英寸到釐米,米
- 5. 什麼是標準pdf比例尺(毫米,釐米,英寸)?
- 6. 如何使用單位像英寸和毫米工作織物JS
- 7. 函數將毫米倒圓到最近的32英寸
- 8. 釐米到英尺和英寸轉換
- 9. 毫米升壓::單位
- 10. wkhtmltopdf釐米/毫米的css尺寸
- 11. Android:尺寸爲毫米或英寸的視圖尺寸不正確
- 12. 從英尺到米的單位換算
- 13. 以釐米(英寸)爲單位的Div寬度
- 14. 使用物理單位的Android Webview(釐米/英寸)
- 15. .net圖表控件 - 以毫米爲單位顯示值:ss
- 16. 如何在matplotlib中以毫米爲單位設置軸的尺寸和位置?
- 17. 米到英尺
- 18. 高度轉換 - 釐米到英尺和英寸(反之亦然)
- 19. TCPDF創建標籤PDF尺寸寬度:57毫米和高度:32毫米
- 20. 如何以毫米爲單位獲取屏幕尺寸使用C#
- 21. JAVA - 顯示英制轉換爲米。每12英寸輸出一個空行。 (一米等於大約39.37英寸)
- 22. 將釐米轉換爲英寸
- 23. 將英寸轉換爲釐米
- 24. F#測量單位建模公制前綴(微米,毫米,納米)
- 25. 我需要將QGraphicsScene打印爲實際(英寸/毫米)的刻度
- 26. 只得到了日期時間毫米DD YYY HH毫米SS
- 27. 如何將報表設計器標尺單位從英寸更改爲釐米?
- 28. iOS按鈕位置x,y 3,5英寸到4英寸
- 29. 差異毫米
- 30. PrintDocument_PrintPage&Graphics.DrawImage大小以英寸爲單位
出了什麼問題'*'和'/'? – Bobby 2011-05-17 07:37:27
沒有錯,但如果有這樣的類,爲什麼我應該再次發明車輪;) – Tomas 2011-05-17 07:39:01