我有这个程序,从双变量中获取所有数字,删除小数点和减号,并分别添加每个数字.这是:
static void Main(string[] args)
{
double input = double.Parse(Console.ReadLine());
char[] chrArr = input.ToString().ToCharArray();
input = 0;
foreach (var ch in chrArr)
{
string somestring = Convert.ToString(ch);
int someint = 0;
bool z = int.TryParse(somestring, out someint);
if (z == true)
{
input += (ch - '0');
}
}
Run Code Online (Sandbox Code Playgroud)
The problem is for example when I enter "9999999999999999999999999999999...." and so on, it gets represented as 1.0E+254 and what so my program just adds 1+0+2+5+4 and finishes. Is there efficient way to make this work properly ? I tried using string instad of double, but it works too slow..
你不能存储"9999999999999999999999999999999..."为双double精度- 只有15或16位精度.编译器为您提供了它可以代表您所要求的最接近的双倍,即1E254.
我会研究为什么使用string缓慢或使用BigInteger