js如何判断一个数字是正数还是负数

JavaScript010

js如何判断一个数字是正数还是负数,第1张

需要准备的材料分别是:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html的<script>标签中,输入js代码:

var a = 3.14, b = -6

print(a)print(b)

function print(c) {

if (c >0)

document.write(c + ' is positive.<br/>')

else if (c <0)

document.write(c + ' is negative.<br/>')

}

3、浏览器运行index.html页面,此时会打印出数字正负判断的结果。

<input type="number" name="" onkeydown="suibian(event)" />       function suibian(e){

        console.log(e)

        if((e.keyCode>=97&&e.keyCode<=105) ||(e.keyCode>=49&&e.keyCode<=57)){

          e.returnValue = true

          return

        }

        e.returnValue = false

      }

<script type="text/javascript">

var reg = /^[+-]?\d*\.?\d{0,3}$/

alert( reg.test("-0.01234"))//如果是正负小数(保留3位小数)或者正负整数,则弹出true,已验证过

</script>