toFixed实现帮助 [英] toFixed implementation help

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

问题描述

我已经阅读了常见问题和讨论,但我不是Javascript

程序员而且不知道如何制作toFixed(和其他技巧)

工作,之后几次尝试。


以下计算在提交表单时起作用(也许

他们不应该...),但我不知道如何在现有代码中添加一个Rounding函数

,这样每个Result都会被舍入到2个小数位

个位置。


到固定的声音喜欢它会是理想的...


< input type =" submit" value ="提交CCI - 最多10个项目

name =" SubmitCCI001"

onClick ="

this.form ._15_1_Total.value =((this.form._13_1_Quantity.value -

0)*(this.form._14_1_UnitPrice.value - 0));

this.form ._15_2_Total.value =((this.form._13_2_Quantity.value -

0)*(this.form._14_2_UnitPrice.value - 0));

this.form ._15_3_Total.value =((this.form._13_3_Quantity.value -

0)*(this.form._14_3_UnitPrice.value - 0));

this.form ._15_4_Total.value =((this.form._13_4_Quantity.value -

0)*(this.form._14_4_UnitPrice.value - 0));

this.form ._15_5_Total.value =((this.form._13_5_Quantity.value -

0)*(this.form._14_5_UnitPrice.value - 0));

this.form ._15_6_Total.value =((this.form._13_6_Quantity.value -

0)*(this.form._14_6_UnitPrice.value - 0));

this.form ._15_7_Total.value =((this.form._13_7_Quantity.value -

0)*(this.form._14_7_UnitPrice.value - 0));

this.form._15_8_Total.value =((this.form._13_8_Quantity.value -

0)*(this.form._14_8_UnitPrice.value - 0));

this.form._15_9_Total.value =((this.form._13_9_Quantity.value -

0)*(this.form._14_9_UnitPrice.value - 0));

this.form._15_10_Total.value =((this.form._13_10_Quantity.value

- 0)*(this.form._14_10_UnitPrice.value - 0));

this.form._17_InvoiceTotal.value =((this.form._15_1_Total.value - 0)+

(this.form._15_2_Total.value - 0)+(this.form._15_3_Total.value - 0)+

(this.form._15_4_Total.value - 0)+(this。 form._15_5_Total.value - 0)+

(this.form._15_6_Total.value - 0)+(this.form._15_7_Total.value - 0)+

( this.form._15_8_Total.value - 0)+(this.form._15_9_Total.value - 0)+

(this.form._15_10_Total.value - 0));

">


感谢您的耐心,Bryan

I have read the FAQ and the discussions but I am not a Javascript
programmer and do not know how to make toFixed (and other techniques)
work, after several attempts.

The following calculations work when the form is submitted (and maybe
they should not...), but I do not know how to add a Rounding function
to the existing code, so that each Result is Rounded to 2 decimal
places.

toFixed sounds like it would be ideal...

<input type="submit" value="Submit CCI - for Maximum of 10 Items"
name="SubmitCCI001"
onClick="
this.form._15_1_Total.value = ((this.form._13_1_Quantity.value -
0) * (this.form._14_1_UnitPrice.value - 0));
this.form._15_2_Total.value = ((this.form._13_2_Quantity.value -
0) * (this.form._14_2_UnitPrice.value - 0));
this.form._15_3_Total.value = ((this.form._13_3_Quantity.value -
0) * (this.form._14_3_UnitPrice.value - 0));
this.form._15_4_Total.value = ((this.form._13_4_Quantity.value -
0) * (this.form._14_4_UnitPrice.value - 0));
this.form._15_5_Total.value = ((this.form._13_5_Quantity.value -
0) * (this.form._14_5_UnitPrice.value - 0));
this.form._15_6_Total.value = ((this.form._13_6_Quantity.value -
0) * (this.form._14_6_UnitPrice.value - 0));
this.form._15_7_Total.value = ((this.form._13_7_Quantity.value -
0) * (this.form._14_7_UnitPrice.value - 0));
this.form._15_8_Total.value = ((this.form._13_8_Quantity.value -
0) * (this.form._14_8_UnitPrice.value - 0));
this.form._15_9_Total.value = ((this.form._13_9_Quantity.value -
0) * (this.form._14_9_UnitPrice.value - 0));
this.form._15_10_Total.value = ((this.form._13_10_Quantity.value
- 0) * (this.form._14_10_UnitPrice.value - 0));
this.form._17_InvoiceTotal.value = ((this.form._15_1_Total.value - 0) +
(this.form._15_2_Total.value - 0) + (this.form._15_3_Total.value - 0) +
(this.form._15_4_Total.value - 0) + (this.form._15_5_Total.value - 0) +
(this.form._15_6_Total.value - 0) + (this.form._15_7_Total.value - 0) +
(this.form._15_8_Total.value - 0) + (this.form._15_9_Total.value - 0) +
(this.form._15_10_Total.value - 0));
">

Thank you for your patience, Bryan

推荐答案

BC写道:
BC wrote:
< input type =" submit" value ="提交CCI - 最多10个项目
name =" SubmitCCI001"
onClick ="
this.form._15_1_Total.value =((this.form._13_1_Quantity .value -
0)*(this.form._14_1_UnitPrice.value - 0));
this.form._15_2_Total.value =((this.form._13_2_Quantity.value -
0 )*(this.form._14_2_UnitPrice.value - 0));
this.form._15_3_Total.value =((this.form._13_3_Quantity.value -
0)*(this.form._14_3_UnitPrice .value - 0));
this.form._15_4_Total.value =((this.form._13_4_Quantity.value -
0)*(this.form._14_4_UnitPrice.value - 0));
this.form._15_5_Total.value =((this.form._13_5_Quantity.value -
0)*(this.form._14_5_UnitPrice.value - 0));
this.form._15_6_Total .value =((this.form._13_6_Quantity.value -
0)*(this.form._14_6_UnitPrice.value - 0));
this.form._15_7_Total.value =((this.form ._13_7_Quantity.value -
0)*(this.form._14_7_UnitPrice.value - 0));
this.form._15_8_Total.value =((this.form._13_8_Quantity.value -
0)*(this.form._14_8_UnitPrice.value - 0));
this.form._15_9_Total.value =((this.form._13_9_Quantity.value -
0)*(this.form._14_9_UnitPrice.value - 0));
this.form._15_10_Total.value = ((this.form._13_10_Quantity.value
- 0)*(this.form._14_10_UnitPrice.value - 0));
this.form._17_InvoiceTotal.value =((this.form._15_1_Total。值 - 0)
+(this.form._15_2_Total.value - 0)+(this.form._15_3_Total.value -
0)+(this.form._15_4_Total.value - 0)+( this.form._15_5_Total.value
- 0)+(this.form._15_6_Total.value - 0)+
(this.form._15_7_Total.value - 0)+(this.form._15_8_Total。值 - 0)
+(this.form._15_9_Total.value - 0)+(this.form._15_10_Total.value -
0)); ">
<input type="submit" value="Submit CCI - for Maximum of 10 Items"
name="SubmitCCI001"
onClick="
this.form._15_1_Total.value = ((this.form._13_1_Quantity.value -
0) * (this.form._14_1_UnitPrice.value - 0));
this.form._15_2_Total.value = ((this.form._13_2_Quantity.value -
0) * (this.form._14_2_UnitPrice.value - 0));
this.form._15_3_Total.value = ((this.form._13_3_Quantity.value -
0) * (this.form._14_3_UnitPrice.value - 0));
this.form._15_4_Total.value = ((this.form._13_4_Quantity.value -
0) * (this.form._14_4_UnitPrice.value - 0));
this.form._15_5_Total.value = ((this.form._13_5_Quantity.value -
0) * (this.form._14_5_UnitPrice.value - 0));
this.form._15_6_Total.value = ((this.form._13_6_Quantity.value -
0) * (this.form._14_6_UnitPrice.value - 0));
this.form._15_7_Total.value = ((this.form._13_7_Quantity.value -
0) * (this.form._14_7_UnitPrice.value - 0));
this.form._15_8_Total.value = ((this.form._13_8_Quantity.value -
0) * (this.form._14_8_UnitPrice.value - 0));
this.form._15_9_Total.value = ((this.form._13_9_Quantity.value -
0) * (this.form._14_9_UnitPrice.value - 0));
this.form._15_10_Total.value = ((this.form._13_10_Quantity.value
- 0) * (this.form._14_10_UnitPrice.value - 0));
this.form._17_InvoiceTotal.value = ((this.form._15_1_Total.value - 0)
+ (this.form._15_2_Total.value - 0) + (this.form._15_3_Total.value -
0) + (this.form._15_4_Total.value - 0) + (this.form._15_5_Total.value
- 0) + (this.form._15_6_Total.value - 0) +
(this.form._15_7_Total.value - 0) + (this.form._15_8_Total.value - 0)
+ (this.form._15_9_Total.value - 0) + (this.form._15_10_Total.value -
0)); ">




< input type =" submit" value ="提交CCI - 最多10个项目

name =" SubmitCCI001" onclick =" calculateForm(this.form)"

[...]

函数calculateForm(oForm){

var i,sum = 0,qty,prc,sbt;

for(i = 1; i< = 10; i ++){

qty = oForm.elements [" _13_" + i +_Quantity]。值;

prc = oForm.elements [" _14_" + i +" _UnitPrice"]。value;

sbt =(isNaN(qty)|| isNaN(prc))? 0:toFixed2(qty * prc);

oForm.elements [" _15_" + i +" _Total"]。value = sbt;

sum + = sbt;

}

oForm.elements [" _17_InvoiceTotal .value"] = toFixed2(sum);

}


函数toFixed2(x){

if(Number() .toFixed)返回Number(x).toFixed(2);

var k =(Math.round(x * 100)/ 100).toString();

k + =(k.indexOf(''。'')== -1)? ''。00'':'00'';

返回k.substring(0,k.indexOf(''。'')+ 3);

}


ciao,dhgm



<input type="submit" value="Submit CCI - for Maximum of 10 Items"
name="SubmitCCI001" onclick="calculateForm(this.form)">
[...]
function calculateForm(oForm) {
var i, sum = 0, qty, prc, sbt;
for (i=1; i<=10; i++) {
qty = oForm.elements["_13_" + i + "_Quantity"].value;
prc = oForm.elements["_14_" + i + "_UnitPrice"].value;
sbt = (isNaN(qty) || isNaN(prc))? 0 : toFixed2(qty * prc);
oForm.elements["_15_" + i + "_Total"].value = sbt;
sum += sbt;
}
oForm.elements["_17_InvoiceTotal.value"] = toFixed2(sum);
}

function toFixed2(x) {
if (Number().toFixed) return Number(x).toFixed(2);
var k = (Math.round(x * 100) / 100).toString();
k += (k.indexOf(''.'') == -1)? ''.00'' : ''00'';
return k.substring(0, k.indexOf(''.'') + 3);
}

ciao, dhgm


感谢Dietmar试图让我走出黑暗时代。

我将函数复制到< Head>

< script type =" text / javascript">

< / script>之间的HTML标签

http://www.iconcustoms。 com / cci-001.html

它不会计算。

干杯,布莱恩

Thank you Dietmar for trying to bring me out of the dark ages.
I copied the functions into the <Head> of the HTML between
<script type="text/javascript">
</script> tags
at http://www.iconcustoms.com/cci-001.html
and it will not compute.
Cheers, Bryan


JRS:文章< 3d ************* @ individual.net>,4月27日星期三

2005 18:14:09 ,在新闻中看到:comp.lang.javascript,Dietmar Meier

< us *************** @ innoline-systemtechnik.de>发布:
JRS: In article <3d*************@individual.net>, dated Wed, 27 Apr
2005 18:14:09, seen in news:comp.lang.javascript, Dietmar Meier
<us***************@innoline-systemtechnik.de> posted :

函数toFixed2(x){
if(Number()。toFixed)返回Number(x).toFixed(2);
var k = (Math.round(x * 100)/ 100).toString();
k + =(k.indexOf(''。'')== -1)? ''。00'':''00'';
返回k.substring(0,k.indexOf(''。'')+ 3);
}

function toFixed2(x) {
if (Number().toFixed) return Number(x).toFixed(2);
var k = (Math.round(x * 100) / 100).toString();
k += (k.indexOf(''.'') == -1)? ''.00'' : ''00'';
return k.substring(0, k.indexOf(''.'') + 3);
}



(a)因为必须提供no-toFixed案例的代码(因为toFixed是

本身不够广泛)并且因为很可能结果

会显示出来,所以计算速度并不重要,是不是真的好b / b
值得一共来打扰?


(b )toFixed可靠地说有bug,但我不知道是否有任何

适用于toFixed(2) -

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


(c)对于非常大的数字,编码转换将失败,其中

toString使用电子格式;如果数字可能非常大,那么k可以是

如果它包含一个e(更好地给出正确的数字,而不是给出错误的格式,而不是给出错误的 - 或者非数字,恕我直言。


-

?约翰斯托克顿,英国萨里。 ?@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.htm> jscr数学,日期,来源。

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



(a) as code for the no-toFixed case must be provided (because toFixed is
not widely enough native) and as it is very probable that the result
will be displayed and so calculation speed is not vital, is it really
worth bothering with toFixed at all?

(b) toFixed is reliably said to have bugs, but I don''t know whether any
apply to toFixed(2) -
<URL:http://www.merlyn.demon.co.uk/js-round.htm#toF>

(c) the coded conversion will fail for very large numbers, where
toString uses e-format; if numbers may be very large, then k could be
returned as result if it contains an e (better to give right number in
wrong format than to give wrong- or non- number, IMHO).

--
? 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


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

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