我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

Prototype1.6 实战 09 (Number)

<HTML>
 
<HEAD>
  
<TITLE>Number</TITLE>
<script src="prototype.js" type="text/javascript"></script>
<script>
    
function abs$()
    
{
        
var v = parseInt($('n1').value);
        
//(-1).abs()方法相当于Math.abs(-1);
        alert(Math.abs(v) + " | " + v.abs());
    }

    
//ceil()方法
    function ceil$()
    
{
        
var v = parseFloat($('n2').value);
        alert(v 
+ ":::" + v.ceil());
    }

    
//floor()方法
    function floor$()
    
{
        
var v = parseFloat($('n3').value);
        alert(v.floor());
    }

    
//四舍五入
    function round$()
    
{
        
var v = parseFloat($('n4').value);
        alert(v.round());
    }

    
//返回当前数字加1
    function succ$()
    
{
        
var v = parseFloat($('n5').value);
        alert(v.succ());
    }

    
//表示循环几次,不包括当前数字
    function times$()
    
{
        
var v = parseInt($('n6').value);
        
var t = '';
        v.times(
function(n){
            t 
+= n;
        }
);
        alert(t);
    }

    
//装饰方法
    function toPaddedString()
    
{
        
var v =  parseInt($('n7').value);
        
//不足4位前面补0
        alert(v.toPaddedString(4));
        
//第二个参数为输出形式为几进制,默认不传为10进制
        alert(v.toPaddedString(4,2));

    }

</script>
 
</HEAD>

 
<BODY>
    
<input type="text" id="n1" value="-5"/>
    
<input type="button" onclick="abs$();" value="绝对值" />
    
<br />
    
<input type="text" id="n2" value="4.1"/>
    
<input type="button" onclick="ceil$();" value="Ceil" />
    
<br />
    
<input type="text" id="n3" value="4.1"/>
    
<input type="button" onclick="floor$();" value="Floor" />
    
<br />
    
<input type="text" id="n4" value="4.1"/>
    
<input type="button" onclick="round$();" value="round" />
    
<br />
    
<input type="text" id="n5" value="4"/>
    
<input type="button" onclick="succ$();" value="succ" />
    
<br />
    
<input type="text" id="n6" value="5"/>
    
<input type="button" onclick="times$();" value="times" />
    
<br />
    
<input type="text" id="n7" value="5"/>
    
<input type="button" onclick="toPaddedString();" value="toPaddedString" />
 
</BODY>
</HTML>


posted on 2008-07-25 14:53 々上善若水々 阅读(223) 评论(0)  编辑  收藏 所属分类: JavaScript


只有注册用户登录后才能发表评论。


网站导航: