C#中有一些示例转换代码是否从十进制度转为度,分,秒,十?将十进制度转换为十分之几秒。
6
A
回答
8
我认为这应该做到这一点。
double decimal_degrees;
// set decimal_degrees value here
double minutes = (decimal_degrees - Math.Floor(decimal_degrees)) * 60.0;
double seconds = (minutes - Math.Floor(minutes)) * 60.0;
double tenths = (seconds - Math.Floor(seconds)) * 10.0;
// get rid of fractional part
minutes = Math.Floor(minutes);
seconds = Math.Floor(seconds);
tenths = Math.Floor(tenths);
27
这是我前一堂课的课。
public class GeoAngle
{
public bool IsNegative { get; set; }
public int Degrees { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public int Milliseconds { get; set; }
public static GeoAngle FromDouble(double angleInDegrees)
{
//ensure the value will fall within the primary range [-180.0..+180.0]
while (angleInDegrees < -180.0)
angleInDegrees += 360.0;
while (angleInDegrees > 180.0)
angleInDegrees -= 360.0;
var result = new GeoAngle();
//switch the value to positive
result.IsNegative = angleInDegrees < 0;
angleInDegrees = Math.Abs(angleInDegrees);
//gets the degree
result.Degrees = (int)Math.Floor(angleInDegrees);
var delta = angleInDegrees - result.Degrees;
//gets minutes and seconds
var seconds = (int)Math.Floor(3600.0 * delta);
result.Seconds = seconds % 60;
result.Minutes = (int)Math.Floor(seconds/60.0);
delta = delta * 3600.0 - seconds;
//gets fractions
result.Milliseconds = (int)(1000.0 * delta);
return result;
}
public override string ToString()
{
var degrees = this.IsNegative
? -this.Degrees
: this.Degrees;
return string.Format(
"{0}° {1:00}' {2:00}\"",
degrees,
this.Minutes,
this.Seconds);
}
public string ToString(string format)
{
switch (format)
{
case "NS":
return string.Format(
"{0}° {1:00}' {2:00}\".{3:000} {4}",
this.Degrees,
this.Minutes,
this.Seconds,
this.Milliseconds,
this.IsNegative ? 'S' : 'N');
case "WE":
return string.Format(
"{0}° {1:00}' {2:00}\".{3:000} {4}",
this.Degrees,
this.Minutes,
this.Seconds,
this.Milliseconds,
this.IsNegative ? 'W' : 'E');
default:
throw new NotImplementedException();
}
}
}
5
0
你可以简单地使用这2个功能:
public Tuple<int,int,int> DecimalToDegrees(decimal decimalValue)
{
return Tuple.Create(Convert.ToInt32(decimal.Truncate(decimalValue)),Convert.ToInt32((decimal.Truncate(Math.Abs(decimalValue)*60))%60),Convert.ToInt32((Math.Abs(decimalValue)*3600)%60));
}
和:
public decimal DecimalToDegrees(int deg , int min , int sec)
{ //~2.3825224324453 Meters error due to accuracy
return deg+(min/60m)+(sec/3600m);
}
相关问题
- 1. 转换度/分/秒到十进制度
- 2. 如何将GPS度数,分钟,秒转换为度,十进制分/秒?
- 3. 将十进制转换为十六进制和十六进制
- 4. 如何在度,分,秒之间转换十进制坐标
- 5. 将十进制坐标转换成度,分,秒,方向
- 6. 由c转换十进制坐标为度,分和秒#
- 7. mysql如何十进制值转换为度,分,秒
- 8. DATEDIFF将分钟转换为十进制
- 9. 将十进制数转换为分数
- 10. 将百分比转换为十进制
- 11. MySQL转换度,分,秒到度十进制
- 12. 将十进制转换为十六进制/二进制
- 13. 将十六进制转换为二进制到十六进制?
- 14. 将八进制数转换为十进制和十六进制
- 15. 将十六进制转换为二进制,然后转换为十进制
- 16. 将十进制转换为小时:分钟:秒
- 17. 使用VBA将十进制数转换为分钟和秒
- 18. 将十进制表示时间转换为小时,分钟,秒
- 19. 将十进制小时转换为小时分秒
- 20. 在Java中将十进制度数或度分秒转换为米
- 21. 将二进制转换为十进制
- 22. 将十进制转换为二进制
- 23. 使用限制将十进制转换为十六进制
- 24. 十六进制转换为十进制电压转换
- 25. 十进制转换为十六进制的转换(Java)的
- 26. 如何将十六进制NSData转换为十进制数组?
- 27. 将2位十进制转换为十六进制
- 28. 在Visual C++中将十进制值转换为十六进制
- 29. 将十进制转换为十六进制值
- 30. 在Python中将十六进制值转换为十进制
到目前为止客场,这是最好的答案。 –
这很好,但我经常看到秒作为一个浮点值,这将窒息在 – Dan