js如何判断小数点后有几位
<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
以上两种方法最通用,但却无法满足某些特殊要求,比如保留小数点后两位,如果不满两位,不满两位则补零。此时就有了第三种方法
- 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 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<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如何判断小数点后有几位的更多相关文章
- 用js正则判断输入的两位字符,第一位是数字16进制的,第二位是I、O、Q、U除外的字母(A到Z)
项目中遇到客户的需求变化,要验证某个数据的正确性,判断输入的两位字符,第一位是数字16进制的,第二位是I.O.Q.U除外的字母(A到Z). 本来对js不熟练,网上参考了一下js正则验证的规则,使用正则 ...
- 金额input框控制只能小数点后有两位的有效数字
<%@include file="/WEB-INF/jsp/common/common.jsp" %> <title>价格录入限定</title> ...
- js判断小数点几位
js如何判断小数点后有几位 <script> var n=3.143423423;alert(n.toString().split(".")[1].length); & ...
- js保留小数点后面几位的方法
原文地址: http://www.jb51.net/article/45884.htm 四舍五入以下处理结果会四舍五入: ? 1 2 var num =2.446242342; num = num.t ...
- js 技巧 (八)JS代码判断集锦(之二)
JS代码判断集锦(之二) <INPUT TYPE="button" value="登录" tabindex="4"> < ...
- iOS textField输入金额的限制,小数点前9位,后面两位
iOS textField输入金额的限制,小数点前9位,后面两位,如果不加小数点,最大位数是9位,加上小数点,最大位数是12位,超出最大位数可删除 - (BOOL)textField:(UITextF ...
- js 技巧 (七)JS代码判断集锦(之一)
JS代码判断集锦(之一) ~~~~~~~~~~~~~~~~~~ <script language="JavaScript"> function checkid(iden ...
- js中判断对象具体类型
大家可能知道js中判断对象类型可以用typeof来判断.看下面的情况 <script> alert(typeof 1);//number alert(typeof "2" ...
- JS代码判断IE6,IE7,IE8,IE9!
JS代码判断IE6,IE7,IE8,IE9!2011年12月15日 星期四 14:01做网页有时候会用到JS检测IE的版本,下面是检测Microsoft Internet Explorer版本的三种代 ...
随机推荐
- Linux tomcat自动启动
1.编辑/etc/rc.d/rc.local 添加环境变量 例如: JAVA_HOME=/usr/local/java/JRE_HOME=/usr/local/java/jreCLASS_PATH=. ...
- C语言基础总结 分类: iOS学习 c语言基础 2015-06-11 10:08 23人阅读 评论(0) 收藏
//欲练此功必先自宫!!! //第一天:C语言的基础 //进制 //2进制, 10进制, 8进制, 16进制 //注:8进制数前加0, 16进制数前加0x ...
- CSS的使用
1.行内样式/内联样式 特点:在具体的HTML标签中引入CSS代码 语法:所有的HTML标签都具有一个style属性,属性值就可以使用CSS样式规则 <标签 style="CSS样式规 ...
- python3 获取当前调用函数名
import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名lineNumber = sys._getframe().f_b ...
- 【分形】【洛谷P1498】
https://www.luogu.org/problemnew/show/P1498 题目描述 自从到了南蛮之地,孔明不仅把孟获收拾的服服帖帖,而且还发现了不少少数民族的智慧,他发现少数民族的图腾往 ...
- Android开发小问题解决汇总
1.从命名文件时报“Read-only file system”255|shell@jacinto6evm:/vendor/lib/hw $ sushell@xxx:/system/vendor/li ...
- WIFI_仿手机写wifi应用程序_WDS
2-1.1_15节_使用WIFI网卡6_仿手机写wifi操作程序============================== 1. 仿手机写一个WIFI操作程序,作为STA,有这几个功能:a. 自动扫 ...
- MySQL--增加或修改列注释
##=============================================## ## 在创建表的时候为字段添加注释 ## CREATE TABLE TB003 ( ID INT P ...
- 廖雪峰 ---- Python教程
这是小白的Python新手教程,具有如下特点: 中文,免费,零起点,完整示例,基于最新的Python 3版本. Python是一种计算机程序设计语言.你可能已经听说过很多种流行的编程语言,比如非常难学 ...
- java 多线程之 线程优先级和守护线程
线程优先级的介绍 java 中的线程优先级的范围是1-10,默认的优先级是5."高优先级线程"会优先于"低优先级线程"执行. java 中有两种线程:用户线程和 ...