格式化计算结果 [英] Formatting the results of a calculation

查看:79
本文介绍了格式化计算结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题让我难倒了好几天了。我有一个表格,我将字段的值加在一起,并在总字段中显示总额

。我的计算工作正常。我的

问题是我想将总数的格式从

1234567更改为1,234,567。这可能使用JavaScript吗?这是我用来进行计算的

代码。我已经删除了用于验证字段的所有代码

,因为我认为不需要

这个问题。


先谢谢。


函数calculateTotal(){

var

fields = Array(" life1",life2,life3,life4,life 5,life6);

for(i = 0; i< fields.length; i ++){

str =" var

val" + i +" = parseInt(document.frmLifeNeed。" + fields [i] +" .value );" ;;

eval(str);

}

total = val0 + val1 + val2 + val3 + val4-val5

document.frmLifeNeed.total.value = total;

}

Josh Austin

系统管理员

美国代理服务

ksadmin NOSPAM @ comcast.net

(删除NOSPAM和我的电子邮件地址以外的空间以发送电子邮件)

I have a question which has had me stumped for a few days now. I have
a form that I add the values of fields together and display the total
in a total field. I have the calculations working correctly. My
problem is that I would like to change the format of the total from
1234567 to 1,234,567 . Is this possible using javascript? Here is the
code that I use to do the calculations. I have edited out all the code
that I use to validate the fields because I don''t think it is needed
for this question.

Thanks in advance.

function calculateTotal(){
var
fields=Array("life1","life2","life3","life4","life 5","life6");
for(i=0;i<fields.length;i++){
str="var
val"+i+"=parseInt(document.frmLifeNeed."+fields[i]+".value);";
eval(str);
}
total=val0+val1+val2+val3+val4-val5
document.frmLifeNeed.total.value = total;
}
Josh Austin
System Administrator
Agent Services of America
ksadmin NOSPAM @comcast.net
(remove NOSPAM and the spaces out of my email address to send email)

推荐答案

" KsAdmin"在总字段中写了
"KsAdmin" wrote
。我的计算工作正常。我的问题是我想将总数的格式从
1234567更改为1,234,567。这可能使用JavaScript吗?这是
in a total field. I have the calculations working correctly. My
problem is that I would like to change the format of the total from
1234567 to 1,234,567 . Is this possible using javascript? Here is the




请参阅:

< script>

var n = 1234567;

n =''''+ n;

var s ='''';

var l = n.length;

while(l> 0){

s ='',''+ n.substring(l-3,l)+ s;

l- = 3;

}

s = s.substring(1);

alert(s);

< / script> ;

HTH

Ivo



See this:
<script>
var n=1234567;
n=''''+n;
var s='''';
var l=n.length;
while(l>0){
s='',''+n.substring(l-3,l)+s;
l-=3;
}
s=s.substring(1);
alert(s);
</script>
HTH
Ivo


似乎无法让这个工作,

当它运行时会弹出一个警告框,其中包含1234567,而不是使用逗号进行计算的

值。

2004年7月6日星期二19:46:24 +0200,Ivo < no@thank.you>写道:
Can''t seem to get this to work,
when it runs it pops an alert box with 1234567 in it instead of the
value of the calculation with the commas in place.
On Tue, 6 Jul 2004 19:46:24 +0200, "Ivo" <no@thank.you> wrote:
" KsAdmin"在总字段中写了
"KsAdmin" wrote
。我的计算工作正常。我的问题是我想将总数的格式从
1234567更改为1,234,567。这可能使用JavaScript吗?这是
in a total field. I have the calculations working correctly. My
problem is that I would like to change the format of the total from
1234567 to 1,234,567 . Is this possible using javascript? Here is the



请看:
< script>
var n = 1234567;
n =''''+ n;
var s ='''';
var l = n.length;
while(l> 0){
s ='',''+ n.substring(l- 3,l)+ s;
l- = 3;
}
s = s.substring(1);
alert(s);
< /脚本>
HTH
Ivo



See this:
<script>
var n=1234567;
n=''''+n;
var s='''';
var l=n.length;
while(l>0){
s='',''+n.substring(l-3,l)+s;
l-=3;
}
s=s.substring(1);
alert(s);
</script>
HTH
Ivo




Josh Austin

系统管理员

代理服务美国

ksadmin NOSPAM @ comcast.net

(删除NOSPAM和我的电子邮件地址以外的空间以发送电子邮件)



Josh Austin
System Administrator
Agent Services of America
ksadmin NOSPAM @comcast.net
(remove NOSPAM and the spaces out of my email address to send email)

KsAdmin写道:


< snip>


我想从
KsAdmin wrote:

<snip>

I would like to change the format of the total from
1234567至1,234,567。这可能使用JavaScript吗?这是我用来进行计算的代码。我已经删除了用于验证字段的所有代码,因为我不认为这个问题需要
1234567 to 1,234,567 . Is this possible using javascript? Here is the
code that I use to do the calculations. I have edited out all the code
that I use to validate the fields because I don''t think it is needed
for this question.




//使用getDecimal()函数获取数字的小数部分。

//如果你确定val,请使用此func。评估为数字。

//比以下更可靠:返回val-Math.floor(val),这应该是

//返回小数部分,但不是''总是。


函数getDecimal(val){

val + ="" ;;

return val.substring(val .indexOf("。"));

}


//使用commaThis()函数转换数值表达式

//用逗号(String)表示的数字,例如10000.25到10,000.25

//如果您确定输入,请使用此函数评估为数字。


函数逗号(条目){

条目= Math.floor(条目)+""

var rex = /( - ?\d +)(\d {3})/

while(rex.test(entry)){

entry = entry.replace(rex,"



// Use getDecimal() function to get the decimal portion of a number.
// Use this func if you are sure "val" evaluates to a number.
// More reliable than: return val-Math.floor(val), which should
// return the decimal portion, but doesn''t always.

function getDecimal(val){
val+="";
return val.substring(val.indexOf("."));
}

// Use commaThis() function to convert a numerical expression
// to a number with commas(a String), e.g. 10000.25 to "10,000.25"
// Use this func if you are sure "entry" evaluates to a number.

function commaThis(entry) {
entry=Math.floor(entry)+""
var rex = /(-?\d+)(\d{3})/
while (rex.test(entry)) {
entry = entry.replace(rex, "


这篇关于格式化计算结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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