js如何判断小数点后有几位
var n=3.143423423;
alert(n.toString().split(".")[1].length);
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
以上两种方法最通用,但却无法满足某些特殊要求,比如保留小数点后两位,如果不满两位,不满两位则补零。此时就有了第三种方法
function roundNumber(number,decimals) {
var newString;// The new rounded number
decimals = Number(decimals);
if (decimals
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 up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) {// If the last digit is 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 == 10) {
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
1.文本框只能输入数字代码(小数点也不能输入)
2.只能输入数字,能输小数点.
3.数字和小数点方法二
4.只能输入字母和汉字
5.只能输入英文字母和数字,不能输入中文
6.只能输入数字和英文chun
7.小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:
8.小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号:
jquery 四舍五入截取字符串
JS 方法:
// 得到字符串的真实长度(双字节换算为两个单字节)
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));
}