Int.TryParse()返回false始终 [英] Int.TryParse() returns false always

查看:960
本文介绍了Int.TryParse()返回false始终的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

 int varOut;
 int.TryParse(txt1.Text, out varOut); // Here txt1.Text = 4286656181793660  

下面txt1.Text是由JavaScript生成的​​随机16位数字这是一个整数。但上面的代码总是返回false即varOut值始终为零。

Here txt1.Text is the random 16 digit number generated by JavaScript which is an integer. But the above code always return false i.e. varOut value is always zero.

我做错了什么?

推荐答案

INT 32位整数)的限制是 -2,147,483,648 2,147,483,647 。你数过大。

The limit for int (32-bit integer) is -2,147,483,648 to 2,147,483,647. Your number is too large.

有关大整数数,例如你的情况,尽量解析使用 long.TryParse (或 Int64.TryParse ,因为的Int64 在C#)来代替。为号码是 -9.2e18到9.2e18范围 *

For large integer number such as your case, try to Parse using long.TryParse (or Int64.TryParse since Int64 is long in C#) instead. The limit for long number is of the range of -9.2e18 to 9.2e18*

long varOut;
long.TryParse(txt1.Text, out varOut); // Here txt1.Text = 4286656181793660  



这应该足以满足你的电话号码,其中只有大约 4.2e15 4,286,656,181,793,660 )。

另外,也可以要考虑使用 decimal.TryParse ,如果你想拥有的十进制数(含分数,精度更高)。

Alternatively, you may want to consider using decimal.TryParse if you want to have decimal number (containing fraction, higher precision).

decimal varOut;
decimal.TryParse(txt1.Text, out varOut); // Here txt1.Text = 4286656181793660  



128位数据类型,以 -7.9e28到7.9e28 28-29 显著的范围位精度,最适合用于涉及金钱的计算。

It is 128-bit data type, with the range of -7.9e28 to 7.9e28, and 28-29 significant digits precision, fits best for any calculation involving money.

和,作为最后的一句话来完成的答案,可能的不安全即可使用双击 - DO的的使用它。虽然双击具有非常高的范围±5.0×10E-324到±1.7×10e308 ,它的精度只约15-16个数字(参考)。

And, as a last remark to complete the answer, it may be unsafe to use double - do not use it. Although double has a very high range of ±5.0 × 10e−324 to ±1.7 × 10e308, its precision is only about 15-16 digits (reference).

double varOut;
double.TryParse(txt1.Text, out varOut); // Not a good idea... since the input number is 16-digit Here txt1.Text = 4286656181793660  

在这种情况下,你的电话号码由16位数字,这是在双击精密的边缘。因此,在一些情况下,可能有错误的结果而告终。只有当你确信你的人数将的最多的15位精度你是安全的使用它。

In this case, your number consists of 16 digits, which is in the borderline of the double precision. Thus, in some cases, you may end up with wrong result. Only if you are sure that your number will be at most 15-digit precision that you are safe to use it.

搜索< BR>
* -9,223,372,036,854,775,808 9,223,372,036,854,775,807

这篇关于Int.TryParse()返回false始终的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆