<script>
var n=3.143423423;
Alert (N.tostring (). Split (".") [1].length];
JS Javascrip intercept after a few decimal places
First, the use of Math.Round
var original=28.453
1)//round "original" to the 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
The second, js1.5 above can be used tofixed (x), you can specify the number to intercept after the decimal point x bit
3)//round "original" to the decimals
var result=original. toFixed(2);//returns 28.45
4)//Round "original" to 1 decimal
var result=original. toFixed(1);//returns 28.5
The above two methods are the most common, but can not meet certain special requirements, such as the retention of two digits after the decimal point, if less than two, less than two bit 0. Now there's a third way
[JavaScript]View Plaincopy
- function Roundnumber (number,decimals) {
- var newstring; //The new rounded number
- Decimals = number (decimals);
- if (Decimals < 1) {
- NewString = (Math.Round (number)). ToString ();
- } Else {
- var numstring = number.tostring ();
- if (Numstring.lastindexof (".") = =-1) {//If there is no decimal Point
- numstring + = "."; //Give it one at the end
- }
- var cutoff = Numstring.lastindexof (".") + decimals; the point at which to truncate the number
- var d1 = number (numstring.substring (cutoff,cutoff+1)); //The value of the last decimal place that we'll end up with
- var d2 = number (numstring.substring (cutoff+1,cutoff+2)); //The next decimal, after the last one we want
- if (D2 >= 5) {//Do we-need to round-all? If not, the string would just be truncated
- if (D1 = = 9 && cutoff > 0) {//If the last digit are 9, find a new cutoff point
- While (Cutoff > 0 && (D1 = = 9 | | IsNaN (d1))) {
- if (d1 ! = ".") {
- cutoff-= 1;
- D1 = number (numstring.substring (cutoff,cutoff+1));
- } Else {
- cutoff-= 1;
- }
- }
- }
- D1 + = 1;
- }
- if (D1 = = ten) {
- numstring = numstring.substring (0, Numstring.lastindexof ("."));
- var roundednum = number (numstring) + 1;
- NewString = roundednum.tostring () + '. ';
- } Else {
- NewString = numstring.substring (0,cutoff) + d1.tostring ();
- }
- }
- if (Newstring.lastindexof (".") = =-1) {//Do this again, to the new string
- NewString + = ".";
- }
- var decs = (newstring.substring (Newstring.lastindexof (".") +1). length;
- For (var i=0;i<decimals-decs;i++) newstring + = "0";
JS can only be judged by numbers and decimal points.
1. Text box can only enter a numeric code (the decimal point can not be entered)
<input onkeyup= "This.value=this.value.replace (/\d/g, ')" onafterpaste= "This.value=this.value.replace (/\D/g, ' ) ">
2. Only the number can be entered and the decimal point will be lost.
<input onkeyup= "if (IsNaN (value)) ExecCommand (' Undo ')" Onafterpaste= "if (IsNaN (value)) ExecCommand (' Undo ')" >
<input name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' Input number only '); this.value= ';}" >
3. Number and decimal method two
<input type=text tvalue= "" ovalue= "" onkeypress= "if (!this.value.match.? \d*?$/)) This.value=this.t_value;else this.tvalue=this.value;if (This.value.match (?: [/^ (?: \. \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+)?)? $/)) This.ovalue=this.value "onblur=" if (!this.value.match (?: [/^ (?: \). \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. Only letters and kanji can be entered
<input onkeyup= "Value=value.replace (/[\d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[\d]/g, ') "maxlength=10 name=" Numbers ">
5. Only English letters and numbers can be entered, not Chinese
<input onkeyup= "Value=value.replace (/[^\w\.\/]/ig, ')" >
6. Only enter numbers and English<font color= "Red" >chun</font>
<input onkeyup= "Value=value.replace (/[^\d|chun]/g, ')" >
7. Only a maximum of two digits after the decimal point (numeric, Chinese can be entered), can not enter letters and operation symbols:
<input onkeypress= "if ((event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/\.\d\d$/.test ( Value)) Event.returnvalue=false ">
8. Only a maximum of two digits after the decimal point (numbers, letters, Chinese can be entered), you can enter the operation symbol:
<input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') ">
jquery Rounding Intercept string
JS Method:
<script type= "Text/javascript" >
Get the true length of the string (double-byte conversion to two single bytes)
function Getstractuallen (schars)
{
Schars.replace (/[^\x00-\xff]/g, "xx"). length/1024+ "bytes";
Math.Round (Schars.replace (/[^\x00-\xff]/g, "xx"). length/1024); this looks so bad.
Return alert (Formatnum (Schars.replace (/[^\x00-\xff]/g, "xx"). length/1024,4));
}
Format decimals, and round. such as: 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 Method:
function Getstractuallen () {
var count=$ ("#sChars"). Val (). length/1024;
Return Math.Round (Count*math.pow (10,4));
}
Reference Link: http://www.cnblogs.com/zangdalei/p/4732548.html
JS How to judge a few after the decimal point