如何在javascript中比较两个20位数字 [英] How to compare two 20 digit numbers in javascript

查看:105
本文介绍了如何在javascript中比较两个20位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨frnds,

我有两个20分的数字,但是在比较它们的时候,它们在javascript中输出了错误的输出。


例如这里是我的代码,

我的secanrio是,


~如果前20位数字的数字大于第二位20位数字

数字,然后它应该返回。


*另一个重要的是,两个20位数字将相同

但仅限最后一位数更改


代码:


< HTML>

< script language =''javascript''> ;

函数numberCheck()

{

var val1 = document.getElementById(''txt1'')。value;

var val2 = document.getElementById(''txt2'')。value;

if(parseInt(val1)parseInt(val2))

alert(" ;更大的)

其他

提醒(正确);


}

< / script>

< body>

< input type = tex t id =" txt1"值= QUOT; 18446744073709551617" size = 25>

< input type = text id =" txt2"值= QUOT; 18446744073709551616" size = 25>

< input type = button onclick =" numberCheck();" value =" check">


< / body>

< / html>


所以这里的txt1值大于txt2值,但它返回正确

警报。

请告诉我为什么它会这样返回,

比较超过20位是否有任何限制

hi frnds,
Im having two 20digit numbers, But while comparing those it
is giiving wrong ouput in javascript.

for example here is my code,
my secanrio is ,

~ If first 20 digit number is greater number than second 20 digit
number ,then it should return.

* And another important thing is, the two 20digit number will be same
but last one digit only change

Code:

<HTML>
<script language=''javascript''>
function numberCheck()
{
var val1=document.getElementById(''txt1'').value;
var val2=document.getElementById(''txt2'').value;
if(parseInt(val1) parseInt(val2))
alert("Greater")
else
alert("Correct");

}
</script>
<body>
<input type=text id="txt1" value="18446744073709551617" size=25>
<input type=text id="txt2" value="18446744073709551616" size=25>
<input type=button onclick="numberCheck();" value="check">

</body>
</html>

So here txt1 value is greater than txt2 value, but it returning Correct
alert.
Please tel me reason why its returning like this,
Is there any restriction for comparing more than 20 digits

推荐答案

ba ***************** @ gmail.com 关于7/28 / 2006 1:44 AM:
ba*****************@gmail.com said the following on 7/28/2006 1:44 AM:

hi frnds,

我有两个20位数字,但在比较它时

在javascript中输出错误的输出。


例如这里是我的代码,

我的secanrio是,


~如果前20位数字的数字大于第二位20位数字

数字,那么它应该返回。


*另一个重要的是,两个20位数将相同

bu t最后一位数只改变
hi frnds,
Im having two 20digit numbers, But while comparing those it
is giiving wrong ouput in javascript.

for example here is my code,
my secanrio is ,

~ If first 20 digit number is greater number than second 20 digit
number ,then it should return.

* And another important thing is, the two 20digit number will be same
but last one digit only change



如果唯一可以改变的是最后一位数,那么简单地比较

最后一个字符,它不会不管数字多长时间。如果它是
存储在一个字符串中。

If the only thing that can change is the last digit then simply compare
the last character and it doesn''t matter how long the "number" is if it
is stored in a string.


< HTML>

< script language = ''javascript''>

函数numberCheck()

{

var val1 = document.getElementById(''txt1'')。值;
<HTML>
<script language=''javascript''>
function numberCheck()
{
var val1=document.getElementById(''txt1'').value;



var lastChar1 = val1.charAt(val1.length-1);

var lastChar1 = val1.charAt(val1.length-1);


var val2 = document 。.getElementById( '' TXT2 '')值;
var val2=document.getElementById(''txt2'').value;



var lastChar2 = val2.charAt(val2.length-1);

现在,只需比较最后2个字符。

var lastChar2 = val2.charAt(val2.length-1);
Now, simply compare the last 2 characters.


if(parseInt(val1)parseInt(val2))
if(parseInt(val1) parseInt(val2))



如果没有第二个参数,不要使用parseInt或者你最终可能会得到意想不到的结果。


if(+ lastChar1> + lastChar2)


< snip>

Don''t use parseInt without the second parameter or you may eventually
get unexpected results.

if (+lastChar1>+lastChar2)

<snip>


>

所以这里txt1值大于txt2值,但它返回正确

警告。

请告诉我为什么它会这样返回,
>
So here txt1 value is greater than txt2 value, but it returning Correct
alert.
Please tel me reason why its returning like this,



浮点数学引入的错误。

The incorrectness introduced by floating point math.


比较超过20位数是否有任何限制
Is there any restriction for comparing more than 20 digits



可能。


-

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /

Probably.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


ba ***************** @ gmail.com 写道:

[...]
ba*****************@gmail.com wrote:
[...]

所以这里的txt1值大于txt2值,但它返回正确

警报。

请告诉我为什么它的原因像这样回来,

比较超过20位数是否有任何限制
So here txt1 value is greater than txt2 value, but it returning Correct
alert.
Please tel me reason why its returning like this,
Is there any restriction for comparing more than 20 digits



是的,绝对。


如果只需要比较最后一位数字,那么Randy已经回答了你的问题
。但是,如果您想将大数字比作

数字,请阅读以下主题:


< URL:
http://groups.google.com/group/comp....73213f5f2e91c5


>
>


>来自该线程:
>From that thread:



"可表示的范围是+ / -1.7976931348623157×10 ^ 308(

数字小到+/- 5×10 ^ -324)。精确整数

值的范围是+/- 9007199254740992(+/- 2 ^ 53)。


请注意,有很多整数可表示的范围,不能用b $ b来表示,从16位数的整数开始。那是

远远超过你对20位数的要求。

-

Rob

"The representable range is +/-1.7976931348623157×10^308 (with
numbers as small as +/-5×10^-324). The range of precise integer
values is +/-9007199254740992 (+/-2^53)."

Note that there are many integers in the representable range that can''t
be represented exactly, starting with integers of 16 digits. That is
well short of your requirement for 20 digits.
--
Rob


嗨兰迪,


仅作为示例,我告诉你最后一个字符,它可以是

中的任何数字号码,所以我想比较整个20位数字和

其他20位数字。


我想知道,为什么不能正确比较。还有其他方式比较



Hi Randy,

For a example only ,i told u the last character, It can be any digit in
the whole number, So i want to compare the whole 20 digit number with
other 20 digit number.

I want know ,why is it not comparing correctly. Is there anyother way
to compare


这篇关于如何在javascript中比较两个20位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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