传递参数 [英] passing arguments

查看:64
本文介绍了传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript例程中遇到问题。我是什么

尝试做的是将数字的大小限制为十位数,但如果数字小于一,则使用

的重要位置。


我在表格上加载一个包含以下内容的方框


form.j.value = numToString(somevaluehere)


我使用了alert语句来确认该值是否传递给了

numToString函数,但是当该数字进一步传递给

Precision函数时它变为零。我已经读过在Javascript中自动将文本字符串转换为浮点数,并且传递给numToString例程的数字

是数学的结果

操作。


感谢任何建议。


TIA


Dan


函数numToString(数字){


//将数值转换为字符串并截断


位数=精确度(数字)


number = number.toString()


var index = number.indexOf("。",0 )


number = number.substring(0,index +(10-digits))


返回号码


}

函数精度(XV){


//用于设置格式精度的GOSUB

//基本语言中的LOG功能是自然日志,基数10转换

因子2.3


//日志基数10的数字是有效数字的数量,即日志

100 = 2


if(XV = 0){


XV = 1


}


Idloc = Math.floor(Math.log(Math.abs(XV))/ 2.302585093)


if(Idloc< 3){


Idloc = 3


}


if(Idloc 10){


Idloc = 10

}


返回Idloc


}


函数numToString(数字){


//将数值转换为字符串并截断

位=精确度(数字)


number = number.toString()


var index = number.indexOf(" ;。,0)


number = number.substring(0,index +(10-digits))


返回数字


}

解决方案

dt ***** @ yahoo.com 写道:


我在javascript例程中遇到问题。我是什么

尝试做的是将数字的大小限制为十位数,但如果数字小于一,则使用

的重要位置。



这里有很多问题要看,请看看J博士的网站

下面的链接,很好在一个地方包括舍入和精确度,

虽然你也可能需要其他建议:


< URL: http://www.merlyn.demon.co.uk/js-rndg1.htm#RoundSF >


我在表单上加载一个包含以下内容的框


form.j .value = numToString(somevaluehere)


我使用了alert语句来确认该值是否传递给了

numToString函数,但是当这个数字更进一步时传递到

精确度函数它变为零。我已经读过在Javascript中自动将文本字符串转换为浮点数,并且传递给numToString例程的数字

是数学的结果

操作。


感谢任何建议。



[...]


function numToString(number){


//将数值转换为字符串并截断


digits =精确(数字)



使用半-colons结束语句,正确阻止代码,缩进2

空格并手动包装代码约70个字符。允许

自动换行几乎总会引入更多必须找到的错误

并修复。


展示你的代码以便它可以直接复制到测试

文档中并且可以正常工作。提供一些测试用例来表明错误

以及你的期望。


number = number.toString()



将数字转换为字符串的另一种方法是在它前面添加一个空字符串




number =''''+ number;


一旦你的数字中有太多的零,toString()就会返回

科学记数法(在browseres中)我测试至少):


(0.000006).toString()== 0.000006

(0.0000006).toString()== 6e-7


您需要在算法中考虑到这一点 - 请参阅上面的链接。


var index = number。 indexOf("。",0)

number = number.substring(0,index +(10-digits))



You可能在这里截断科学记数,而不是数字

本身(见上面的链接)。例如


0.00000061231488 == 6.1231488e-而不是6.1231488e-7


返回编号

}


函数精度(XV){


//用于设置格式精度的GOSUB

//基本语言中的LOG功能是自然日志,基数10转换

因子2.3

//数字的对数基数10是有效数字的数量,即日志

100 = 2

if(XV = 0){



这里你给零值赋值变量XV,它将始终

评估为false。我认为你的意思是:


if(XV == 0){


一个捕捉这些错误的简单方法是写它们以便不变的

位在左边,然后你会得到一个错误而不是沉默的失败

的逻辑:


if(0 = XV){// - error ...所以希望你能找到并解决它。


XV = 1 < br $>
}


Idloc = Math.floor(Math.log(Math.abs(XV))/ 2.302585093)



您可以使用''Math.LN10''代替2.302 ......我不知道

在这里有多大区别。


if(Idloc< 3){

Idloc = 3

}



此部分尝试将整数部分限制为至少3

位数,因此小数部分的最大值为7.将3更改为0到

get小数点后10位。


>

if(Idloc 10){

Idloc = 10

}

返回Idloc

}



-

Rob


RobG在8/13/2006 11:55 PM说:以下内容:

dt ***** @ yahoo.com 写道:


>我在javascript例程中遇到参数问题。我想要做的是将数字的大小限制为十位数,但如果数字小于一,则使用重要位置的




这里有很多问题要看,请看看J博士的网站

链接下面,它很好地涵盖了一个地方的舍入和精度,

虽然你也可能需要其他建议:


< URL: http://www.merlyn.demon.co.uk/js-rndg1。 htm#RoundSF >



您可能想要选择另一个名称来呼叫JRS,因为Dr。 J型在约翰斯托克顿是一个美国职业篮球运动员。

后者,这是我的理解,是他作为John博士发布的原因

而不仅仅是约翰。


< snip>


> digits = Precision(number)



使用分号结束语句



我与Richard和其他人在

过去进行了深入的讨论。除非一个人毫无疑问地知道在哪里插入分号,否则你最好不要让他们离开,因为UA总是会知道它们b $ b想要它们在哪里以及它在哪里'' T。当被要求提供典型时代码

,当分号被删除时缺少分号导致错误

它,我有一个例子,理查德不记得完整的上下文。

但是,有些地方在一行末尾添加一个分号

会导致错误,因为缺少该分号并不是错误。 />

-

兰迪

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

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


JRS:文章< 11 ******************* **@75g2000cwc.googlegroups.c om>,

日期为2006年8月13日星期日20:55:24远程,见于

news:comp.lang.javascript, RobG< rg *** @ iinet.net.auposted:


> dt ***** @ yahoo.com写道:


number =''''+ number;



或字符串(数字)可能更具可读性。


> Idloc = Math.floor(Math.log(Math.abs(XV))/ 2.302585093)


您可以使用''Math.LN10''代替2.302 ......我不知道这是否会产生很大的不同。



2.302585093在几个地方都不准确。因此有一个很高的

风险,如999999.999999或1000000.000001这样的某些数字将会显示为10的幂的错误。如果是这样,结果可能

误差为10倍。


同样适用于Math.LN10,但程度较小;考虑


for(J = -15; J <16; J ++)

document.write(Math.log(Math.pow(10,J) )/Math.LN10," br>")

其中近一半的结果不准确。


ISTM混合字符串和算术需要非常小心

输出转换操作。


OP:阅读新闻组FAQ。

-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://www.jibbering.com/faq/>? JL / RC:新闻常见问题:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htmjscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/& c,常见问题项目,链接。


I am having trouble with arguments in a javascript routine. What I am
trying to do is to limit the size of numbers to ten digits but make use
of the significant places if the number is less than one.

I load a box on a form with the following

form.j.value=numToString(somevaluehere)

I have used alert statements to confirm that the value is passed to the
numToString function, but when the number is further passed to the
Precision function it becomes zero. I had read that text strings are
converted to floating point automatically in Javascript, and the number
that is passed to the numToString routine is the result of math
operations.

Appreciate any advice.

TIA

Dan

function numToString(number){

// Convert numeric values to string and truncate

digits=Precision(number)

number=number.toString()

var index= number.indexOf(".",0)

number= number.substring(0,index+(10-digits))

return number

}
function Precision(XV){

// GOSUB FOR SETTING PRECISION OF FORMAT

// LOG function in Basic language is natural log, base 10 conversion
factor 2.3

// Log base 10 of number is the quantity of significant digits, ie log
100=2

if (XV= 0){

XV= 1

}

Idloc = Math.floor(Math.log(Math.abs(XV)) / 2.302585093)

if (Idloc < 3) {

Idloc = 3

}

if (Idloc 10) {

Idloc = 10

}

return Idloc

}

function numToString(number){

// Convert numeric values to string and truncate

digits=Precision(number)

number=number.toString()

var index= number.indexOf(".",0)

number= number.substring(0,index+(10-digits))

return number

}

解决方案

dt*****@yahoo.com wrote:

I am having trouble with arguments in a javascript routine. What I am
trying to do is to limit the size of numbers to ten digits but make use
of the significant places if the number is less than one.


There are lots of issues to cover here, have a look at Dr J''s site at
the link below, it nicely covers rounding and precision in one spot,
though you may need other advice too:

<URL: http://www.merlyn.demon.co.uk/js-rndg1.htm#RoundSF >

I load a box on a form with the following

form.j.value=numToString(somevaluehere)

I have used alert statements to confirm that the value is passed to the
numToString function, but when the number is further passed to the
Precision function it becomes zero. I had read that text strings are
converted to floating point automatically in Javascript, and the number
that is passed to the numToString routine is the result of math
operations.

Appreciate any advice.

[...]

function numToString(number){

// Convert numeric values to string and truncate

digits=Precision(number)

Use semi-colons to end statements, block code correctly, indent with 2
spaces and manually wrap code at about 70 characters. Allowing
auto-wrapping nearly always introduces more errors that must be found
and fixed.

Present your code so that it can be copied directly into a test
document and will work. Provide some test cases to indicate the error
and what you expected.

number=number.toString()

Another way to convert number to a string is to prepend an empty string
to it:

number = '''' + number;

Once you get too many zeros in the number, toString() returns
scientific notation (in the browseres I tested at least):

(0.000006).toString() ==0.000006
(0.0000006).toString() ==6e-7

You need to account for that in your algorithm - see the link above.

var index= number.indexOf(".",0)
number= number.substring(0,index+(10-digits))

You may be truncating the scientific notation here, not the number
itself (see the link above). e.g.

0.00000061231488 ==6.1231488e- instead of 6.1231488e-7

return number
}
function Precision(XV){

// GOSUB FOR SETTING PRECISION OF FORMAT
// LOG function in Basic language is natural log, base 10 conversion
factor 2.3
// Log base 10 of number is the quantity of significant digits, ie log
100=2
if (XV= 0){

Here you assign the value of zero to the variable XV, it will always
evaluate to false. I think you meant:

if (XV == 0){

A simple way to catch such errors is write them so that the invariable
bit is on the left, then you''ll get an error rather than silent failure
of logic:

if ( 0 = XV ){ // --error... so hopefully you''ll find and fix it.

XV= 1
}

Idloc = Math.floor(Math.log(Math.abs(XV)) / 2.302585093)

You could use ''Math.LN10'' instead of 2.302... I don''t know whether
that makes much difference here.

if (Idloc < 3) {
Idloc = 3
}

This part here attempts to restrict the integer part to at least 3
digits, hence the decimal part has a maximum of 7. Change 3 to 0 to
get 10 places after the decimal point.

>
if (Idloc 10) {
Idloc = 10
}
return Idloc
}


--
Rob


RobG said the following on 8/13/2006 11:55 PM:

dt*****@yahoo.com wrote:

>I am having trouble with arguments in a javascript routine. What I am
trying to do is to limit the size of numbers to ten digits but make use
of the significant places if the number is less than one.



There are lots of issues to cover here, have a look at Dr J''s site at
the link below, it nicely covers rounding and precision in one spot,
though you may need other advice too:

<URL: http://www.merlyn.demon.co.uk/js-rndg1.htm#RoundSF >

You may want to choose another name to call JRS, as "Dr. J" was a
professional basketball player in the USA as was John Stockton. The
latter, it is my understanding, is the reason he posts as Dr. John
instead of just John.

<snip>

>digits=Precision(number)


Use semi-colons to end statements

I have had that discussion, in depth, with Richard and others in the
past. Unless a person knows, beyond doubt, where to insert semicolons,
you are better off leaving them out as the UA will always know where it
wants them and where it doesn''t. When asked to provide "typical" code
where the lack of a semicolon caused an error when the semicolon removed
it, I got one example and Richard couldn''t remember the full context.
But, there are places that adding a semi-colon to the end of a line will
cause errors where the lack of that semi-colon isn''t an error.

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


JRS: In article <11*********************@75g2000cwc.googlegroups.c om>,
dated Sun, 13 Aug 2006 20:55:24 remote, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auposted :

>dt*****@yahoo.com wrote:

number = '''' + number;

Or String(number) might be more readable.

>Idloc = Math.floor(Math.log(Math.abs(XV)) / 2.302585093)


You could use ''Math.LN10'' instead of 2.302... I don''t know whether
that makes much difference here.

2.302585093 is inexact, by several places. There is therefore a high
risk that some number such as 999999.999999 or 1000000.000001 will
appear to be the wrong side of its power of 10. If so, the result may
be in error by a factor of 10.

The same applies with Math.LN10, to a lesser extent; consider

for (J=-15; J<16; J++)
document.write(Math.log(Math.pow(10, J))/Math.LN10, "<br>")

for which nearly half the results are inexact low.

ISTM that considerable care is needed in mixing string and arithmetic
operations in output conversion.

OP : read the newsgroup FAQ.
--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.


这篇关于传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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