javaScript内置对象Number

Number
静态对象,不需要实例,直接就调用对象名访问
<script>
alert(Number.MAX_VALUE);
</script>

属性:

constructor();
返回创建对象Boolean函数的引用

例:
<script>
var test=new Boolean();
if (test.constructor==Array)
{document.write("This is an Array");}
if (test.constructor==Boolean)
{document.write("This is a Boolean");}
if (test.constructor==Date)
{document.write("This is a Date");}
if (test.constructor==String)
{document.write("This is a String");}
</script>

MAX_VALUE();
表示js中最大的数。近似值:1.7976931348623157 x 10308

例:
<script>
document.write(Number.MAX_VALUE);
</script>
结果:
1.7976931348623157e+308

MIN_VALUE();
表示js中最小的数。接近0,但不是负数,近似值:

5 x 10-324
例:
<script>
document.write(Number.MIN_VALUE);
</script>
结果:5e-324

NaN();
代表非数字的特殊值,用于表示某个值不是数字。

例:
<script>
var Month=30;
if (Month < 1 || Month > 12)
{Month = Number.NaN;}
document.write(Month);
</script>
结果:NaN 

NEGATIVE_INFINITY();
负无穷大,溢出时返回该值。

例:
<script>
var x=(-Number.MAX_VALUE)*2
if (x==Number.NEGATIVE_INFINITY)
  {  document.write("Value of x: " + x);  }
</script>
结果:Value of x: -Infinity

 POSITIVE_INFINITY();
正无穷大,溢出时返回该值。

例:
<script>
var x=(Number.MAX_VALUE)*2
if (x==Number.POSITIVE_INFINITY)
  {  document.write("Value of x: " + x);  }
</script>
结果:Value of x:Infinity

方法:

toString();
把数字转化成字符串。

例:
<script>
var number = new Number(1337);
document.write (number.toString())
</script>
结果:1337

 toLocaleString();
把数字转化成字符串,使用本地数字格式顺序

toFixed();
数字转化成字符串,四舍五入指定小数的位数。
例:
<script>
var num = new Number(13.37);
document.write (num.toFixed(1))
</script>
结果:13.4 

toExponential();
把对象的值转化为指数。

例:
<script>
var num = new Number(10000);
document.write (num.toExponential(1))
</script>
结果:1.0e+4

toPrecision();
指定对象的值,超出的位数将其转化为指数计数法

例:
<script>
var num = new Number(10000);
document.write (num.toPrecision(4))
</script>
结果:1.000e+4

 valueOf();
返回一个对象的基本数字值

猜你喜欢

转载自blog.csdn.net/abenazhan/article/details/77162476
今日推荐