贝尔函数传递一个字符串"1004233"并输出以下输出:
D1 = 1.004233
D2 = 0.00423299999999993
D3 = 4232.99999999993
D4 = 4232
我需要D4打印4233而不是4232.如何阻止这种精确损失的发生?
public string someFunc(String s){
string retval = "0";
try{
int id = int.Parse(s);
double d = (double)id / (double)1000000;
Console.WriteLine("D1 = " + d);
d = d - Math.Truncate(d);
Console.WriteLine("D2 = " + d);
d = d * (double)1000000;
Console.WriteLine("D3 = " + d);
retval = "" + Math.Truncate(d);
Console.WriteLine("D4 = " + retval);
}catch(Exception ex){}
return retval;
}
Run Code Online (Sandbox Code Playgroud)