Showing posts with label Number. Show all posts
Showing posts with label Number. Show all posts

Tuesday, June 10, 2008

Number toString 方法使用

Number.toString(radix)
Converts a number to a string, using a specified radix (base), and returns the string. radix must be between 2 and 36. If omitted, base 10 is used.
比如中文“一”字的unicode编码为\u4e00,要想得到对应的unicode十进制编码,在js中不必用4 * 16 * 16 * 16 + 14 * 16 + 0 + 0 = 19968来获得,可以直接用Number.toString()方法得到:
(0x4e00).toString(10) = "19968"
这样得到的值可以再用js加上前后缀成为"&#" + "19968" + ";" = "一"后可以直接从网页中输出。
反过来,如果知道十进制的一个数字,也可以很容易转换成16进制的值:
(19968).toString(16) = "4e00"

Sunday, August 05, 2007

prototype javascript library Number extends example


<script type="text/javascript" charset="utf-8">
function doc (argument) {
document.write(argument);
document.write("<br />\n");
}

doc((255).toColorPart());
doc((3).succ());
(5).times(function (i) {
doc(i);
})
</script>