只为成功找方向,不为失败找借口

每天都不能停止前进的脚步
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JavaScript中类型的一些方法

Posted on 2009-07-15 16:10  冰碟  阅读(245)  评论(0编辑  收藏  举报

Number类型的几个方法

1,toFixed()

返回具有指定位数小数的数字

var oValue = new Number(99);
alert(oValue.toFixed(
2))    //output 99.00

2,toExponential()

返回用科学计数法表示的数字的字符串形式

var oValue = new Number(99);
alert(oValue.toExponential(
1)); //output 9.9e+1

3,toPrecision()

根据最有意义的形式返回数字的预定形式或指数形式

var oValue = new Number(99);
alert(oValue.toPrecision(
1));  //output 1e+2
alert(oValue.toPrecision(2));  //output 99
alert(oValue.toPrecision(3));  //output 99.0

 

以上三个方法都会进行舍入操作。

字符串类型的几个方法:

1,charAt和charCodeAt

访问字符串中的单个字符或字符代码

var oValue = new String("hello");
alert(oValue.chatAt(
1));          //output e
alert(oValue.chatCodeAt(1));      //output 101

 

2,silce和substring

var oValue = new String("hello world");
alert(oValue.slice(
-3));                  //output rld
alert(oValue.substring(-3));              //output hello world
alert(oValue.slice(3,-4));                //output lo w
alert(oValue.substring(3,-4));            //output hel

 

Globald对象

URI方法encodeURI、encodeURIComponent、decodeURI和decodeURIComponent代替了BOM的escape()和unescape()方法。URI方法更可取,因为它们会对所有Unicode符号变吗,而BOM方法只能对ASCII符号正确编码。尽量避免使用escape()和unescape()方法。