Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用小数填充零_C#_.net_Math - Fatal编程技术网

C# 用小数填充零

C# 用小数填充零,c#,.net,math,C#,.net,Math,我试图得到大于和小于的粗略值来比较纬度和经度。我需要能够传递纬度、经度和要舍入的位数。例如,如果我有这些值: 18.363285 -67.18024 18.448619 -67.13422 <----- Only second value matched 18.498987 -67.13699 <----- Both values matched 18.465162 -67.141486 18.182151 -66.9588 使用M

我试图得到大于和小于的粗略值来比较纬度和经度。我需要能够传递纬度、经度和要舍入的位数。例如,如果我有这些值:

18.363285   -67.18024
18.448619   -67.13422        <----- Only second value matched
18.498987   -67.13699        <----- Both values matched 
18.465162   -67.141486
18.182151   -66.9588
使用Math.Abs:

if (Math.Abs(value1 - value2) <= 0.01) {
    // close enough
    // etc...
}
if(Math.Abs(value1-value2)使用Math.Abs:

if (Math.Abs(value1 - value2) <= 0.01) {
    // close enough
    // etc...
}

if(Math.Abs(value1-value2)那么
4.0/100000.0
呢?是的,但是5/100000不返回0.000005。我有点想使用
Convert.ToString(可枚举的重复(“0”,numDigits))
但这感觉不对。@Echilon
5/100000d
。您需要确保使用的是双精度,否则最终会导致整数数学被截断。您可以通过附加“d”来指定整个值中的一个为双精度。这仍然不能满足我的需要。我已经更新了问题。@Echilon现在问题更清楚了。请参见下面的答案。
4.0/100000.0
?是的,但是5/100000不会返回0.000005。我很想只使用
Convert.ToString(Enumerable.Repeat(“0”,numDigits))
但这感觉不对。@Echilon
5/100000d
。你需要确保你使用的是双精度运算,否则你会得到截断的整数数学。你可以通过附加“d”来指定整个值中的一个是双精度运算。这仍然不能满足我的需要。我已经更新了问题。@Echilon现在更清楚了。请参阅下面的答案。
    private double Calc(int places)
    {
        return 1 / (Math.Pow(10, places + 1));
    }