1. 程式人生 > >js如何判斷小數點後有幾位

js如何判斷小數點後有幾位

br0 get ima data hang mst start tools lai

<script>  
var n=3.143423423;
alert(n.toString().split(".")[1].length);
</script>
js javascrip 截取小數點後幾位

第一種,利用math.round

var original=28.453
1) //round "original" to two decimals
var result=Math.round(original*100)/100; //returns 28.45
2) // round "original" to 1 decimal
var result=Math.round(original*10)/10; //returns 28.5

第二種,js1.5以上可以利用toFixed(x) ,可指定數字截取小數點後 x位

3) //round "original" to two decimals
var result=original.toFixed(2); //returns 28.45

4) // round "original" to 1 decimal
var result=original.toFixed(1); //returns 28.5

以上兩種方法最通用,但卻無法滿足某些特殊要求,比如保留小數點後兩位,如果不滿兩位,不滿兩位則補零。此時就有了第三種方法

[javascript] view plaincopy
  1. function roundNumber(number,decimals) {
  2. var newString;// The new rounded number
  3. decimals = Number(decimals);
  4. if (decimals < 1) {
  5. newString = (Math.round(number)).toString();
  6. } else {
  7. var numString = number.toString();
  8. if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
  9. numString += ".";// give it one at the end
  10. }
  11. var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
  12. var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we‘ll end up with
  13. var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
  14. if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
  15. if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
  16. while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
  17. if (d1 != ".") {
  18. cutoff -= 1;
  19. d1 = Number(numString.substring(cutoff,cutoff+1));
  20. } else {
  21. cutoff -= 1;
  22. }
  23. }
  24. }
  25. d1 += 1;
  26. }
  27. if (d1 == 10) {
  28. numString = numString.substring(0, numString.lastIndexOf("."));
  29. var roundedNum = Number(numString) + 1;
  30. newString = roundedNum.toString() + ‘.‘;
  31. } else {
  32. newString = numString.substring(0,cutoff) + d1.toString();
  33. }
  34. }
  35. if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
  36. newString += ".";
  37. }
  38. var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
  39. for(var i=0;i<decimals-decs;i++) newString += "0";

JS判斷只能是數字和小數點

1.文本框只能輸入數字代碼(小數點也不能輸入)
<input onkeyup="this.value=this.value.replace(/\D/g,‘‘)" onafterpaste="this.value=this.value.replace(/\D/g,‘‘)">

2.只能輸入數字,能輸小數點.
<input onkeyup="if(isNaN(value))execCommand(‘undo‘)" onafterpaste="if(isNaN(value))execCommand(‘undo‘)">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(‘只能輸入數字‘);this.value=‘‘;}">

3.數字和小數點方法二
<input type=text tvalue="" ovalue="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.tvalue=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.ovalue=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.tvalue=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.ovalue=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.ovalue=this.value}">

4.只能輸入字母和漢字
<input onkeyup="value=value.replace(/[\d]/g,‘‘) "onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[\d]/g,‘‘))" maxlength=10 name="Numbers">

5.只能輸入英文字母和數字,不能輸入中文
<input onkeyup="value=value.replace(/[^\w\.\/]/ig,‘‘)">

6.只能輸入數字和英文<font color="Red">chun</font>
<input onKeyUp="value=value.replace(/[^\d|chun]/g,‘‘)">

7.小數點後只能有最多兩位(數字,中文都可輸入),不能輸入字母和運算符號:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">

8.小數點後只能有最多兩位(數字,字母,中文都可輸入),可以輸入運算符號:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,‘$1$2.$3‘)">

jquery 四舍五入截取字符串

JS 方法:

<script type="text/javascript">
// 得到字符串的真實長度(雙字節換算為兩個單字節)
function getStrActualLen(sChars)
{
//sChars.replace(/[^\x00-\xff]/g,"xx").length/1024+"字節";
//Math.round(sChars.replace(/[^\x00-\xff]/g,"xx").length/1024);這個貌似不好使
return alert(formatNum(sChars.replace(/[^\x00-\xff]/g,"xx").length/1024,4));
}
//格式化小數,並四舍五入。如:formatNum(100.12345678,4)
function formatNum(Num1,Num2){
if(isNaN(Num1)||isNaN(Num2)){
return(0);
}else{
Num1=Num1.toString();
Num2=parseInt(Num2);
if(Num1.indexOf(‘.‘)==-1){
return(Num1);
}else{
var b=Num1.substring(0,Num1.indexOf(‘.‘)+Num2+1);
var c=Num1.substring(Num1.indexOf(‘.‘)+Num2+1,Num1.indexOf(‘.‘)+Num2+2);
if(c==""){
return(b);
}else{
if(parseInt(c)<5){
return(b);
}else{
return((Math.round(parseFloat(b)*Math.pow(10,Num2))+Math.round(parseFloat(Math.pow(0.1,Num2).toString().substring(0,Math.pow(0.1,Num2).toString().indexOf(‘.‘)+Num2+1))*Math.pow(10,Num2)))/Math.pow(10,Num2));
}
}
}
}
}

Jquery方法:

function getStrActualLen(){
var count=$("#sChars").val().length/1024;

return Math.round(count*Math.pow(10,4));
}

參考鏈接:http://www.cnblogs.com/zangdalei/p/4732548.html

js如何判斷小數點後有幾位