在javascript里用函数求从1到任意正整数的和怎么写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在javascript里用函数求从1到任意正整数的和怎么写相关的知识,希望对你有一定的参考价值。

前提有一个提示框 var num=window.prompt("请输入一个数",100);
得出来的数是不定的吗..

上面都是循环加的,我就写个递归的吧

function getSum(n)
if(n==0)
return 0;
return n+getSum(n-1);


使用方法:
var num=window.prompt("请输入一个数",100);
num=parseInt(num);
alert(getSum(num));

window.prompt得到的字符串是通过输入确定的,后面的100是默认值,得到的实际是字符串,在数值运算中,字符串和数值得到的结果是不同的.如果想取得转的的数字,需要通过parseInt转换
参考技术A var a=1;
var b=2;

alert(a+b);
参考技术B function getResult(n)

var sum=0;
for(var m=1;m<=n;m++)

sum+=m;

alert("1到"+m+"之间的正整数之和是:"+sum);
参考技术C function add(a)

var i=1;
var sum=1;
for(i=1,i<=a,i++)

sum+=i;

return sum;

javascript的Math.round()函数为啥不能精确小数点位数,

比如: Math.round(3.4442,3); 输出的值是3,而不是3.444,请问怎么能得到小数点后三位数

round 方法, 及四舍五入

返回与给出的数值表达式最接近的整数。
Math.round(number)
必选项 number 参数是要舍入到最接近整数的值。
说明
如果 number 的小数部分大于等于 0.5,返回值是大于 number 的最小整数。否则,round 返回小于等于 number 的最大整数。

而你的写法, 根本就没有用, 没有这样的写法

你自己创建一个方法
function xround(x, num)
Math.round(x * Math.pow(10, num)) / Math.pow(10, num) ;


xround(3.44492, 3); // 得到 3.445
参考技术A 我就不懂了,一个四舍五入,你们那些人何必长篇大论啊。?!楼主看我的。。
Math.round() 这个是四舍五入取整哈。

var number = 2; //或者3.4442
alert(number.toFixed(3)); //toFixed(3)代表四舍五入保留3位小数,当然也可以写2(四舍五入保留2位小数)

鄙视那些到处复制粘贴的人。。
参考技术B 第一种,利用math.round var original=28.453
1) //round "original" to two decimals
var
result=Math.round(original*100)/100; //returns 28.45
2) // round "original"
to 1 decimal
var result=Math.round(original*10)/10; //returns 28.5 第二种,js1.5以上可以利用toFixed(x) ,可指定数字截取小数点后 x位3) //round "original" to two decimals
var result=original.toFixed(2); //returns 28.454) // round "original" to 1 decimal
var result=original.toFixed(1); //returns 28.5 以上两种方法最通用,但却无法满足某些特殊要求,比如保留小数点后两位,如果不满两位,不满两位则补零。此时就有了第三种方法。 第三种,转换函数,这段代码来源于国外一个论坛。 [javascript]view plaincopyprint?function roundNumber(number,decimals) var newString;// The new rounded number decimals = Number(decimals); if (decimals < 1) newString = (Math.round(number)).toString(); else var numString = number.toString(); if (numString.lastIndexOf(".") == -1) // If there is no decimal point numString += ".";// give it one at the end var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
if (d2 >= 5) // Do we need to round up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) // If the last digit is 9, find a new cutoff point
while (cutoff > 0 && (d1 == 9 || isNaN(d1))) if (d1 != ".") cutoff -= 1; d1 = Number(numString.substring(cutoff,cutoff+1)); else cutoff -= 1; d1 += 1; if (d1 == 10) numString = numString.substring(0, numString.lastIndexOf(".")); var roundedNum = Number(numString) + 1; newString = roundedNum.toString() + '.'; else newString = numString.substring(0,cutoff) + d1.toString(); if (newString.lastIndexOf(".") == -1) // Do this again, to the new string
newString += "."; var decs = (newString.substring(newString.lastIndexOf(".")+1)).length; for(var i=0;i<decimals-decs;i++) newString += "0"; //var newNumber = Number(newString);// make it a number if you like
document.roundform.roundedfield.value = newString; // Output the result to the form field (change for your purposes)
function roundNumber(number,decimals)
var newString;// The new rounded number
decimals = Number(decimals);
if (decimals < 1)
newString = (Math.round(number)).toString();
else
var numString = number.toString();
if (numString.lastIndexOf(".") == -1) // If there is no decimal point
numString += ".";// give it one at the end

var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
if (d2 >= 5) // Do we need to round up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) // If the last digit is 9, find a new cutoff point
while (cutoff > 0 && (d1 == 9 || isNaN(d1)))
if (d1 != ".")
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
else
cutoff -= 1;



d1 += 1;

if (d1 == 10)
numString = numString.substring(0, numString.lastIndexOf("."));
var roundedNum = Number(numString) + 1;
newString = roundedNum.toString() + '.';
else
newString = numString.substring(0,cutoff) + d1.toString();


if (newString.lastIndexOf(".") == -1) // Do this again, to the new string
newString += ".";

var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
for(var i=0;i<decimals-decs;i++) newString += "0";
//var newNumber = Number(newString);// make it a number if you like
document.roundform.roundedfield.value = newString; // Output the result to the form field (change for your purposes)


5) //round "original" to two decimals
var result=original.toFixed(2); //returns 28.456) // round "original" to 1 decimal
var result=original.toFixed(1); //returns 28.5 var original=28.4var result=original.toFixed(2); //returns 28.40
参考技术C 最简单的做法就是如下
System.out.println(Math.round(1000*(3.4442))/1000.0);
参考技术D var a = 3.4442;
alert(a.toFixed(3));

以上是关于在javascript里用函数求从1到任意正整数的和怎么写的主要内容,如果未能解决你的问题,请参考以下文章

整数Javascript的锯齿波函数

为啥在Javascript里用window.location.href=‘a.html’语句不能实现网页跳转啊。浏览器还是显示原网页。

R语言plyr包round_any函数将向量数据近似到任意精度实战

请问UG二次开发中如何在c#里用原来的API函数?

用c语言编程,求从1的阶乘一直加到20的阶乘。

javascript生成规定范围的随机整数