用JavaScript中的示例编号toPrecision()方法

Number toPrecision()方法 (Number toPrecision() Method)

toPrecision() method is a method of Number class, it is used to convert a number to a specified length, including the decimal point (if target length is short).

toPrecision()方法是Number类的一种方法,用于将数字转换为指定的长度,包括小数点(如果目标长度短)。

Syntax:

句法:

    Number.toPrecision(n);

Here, n is used to define the length of the number.

在此, n用于定义数字的长度。

Examples:

例子:

    Input: 10
    num = 123.456

    Function call       Output
    num.toPrecision(8)  123.45600
    num.toPrecision(5)  123.45
    num.toPrecision(4)  123.4

Code:

码:

<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
	var num = 123.456;	
	var res1 = num.toPrecision(3);
	var res2 = num.toPrecision(4);
	var res3 = num.toPrecision(5);
	var res4 = num.toPrecision(6);
	var res5 = num.toPrecision(7);
	var res6 = num.toPrecision(8);
	
	document.write("res1 = " + res1 + "<br>");
	document.write("res2 = " + res2 + "<br>");
	document.write("res3 = " + res3 + "<br>");
	document.write("res4 = " + res4 + "<br>");
	document.write("res7 = " + res5 + "<br>");
	document.write("res6 = " + res6 + "<br>");	
</script>
</body>
</html>

Output

输出量

res1 = 123
res2 = 123.5
res3 = 123.46
res4 = 123.456
res7 = 123.4560
res6 = 123.45600


翻译自: https://www.includehelp.com/code-snippets/number-toPrecision-method-with-example-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值