JS How to judge a few after the decimal point

Source: Internet
Author: User
Tags pow

<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
  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-all? If not, the string would just be truncated
  15. if (D1 = = 9 && cutoff > 0) {//If the last digit are 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 = = ten) {
  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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.