<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>js浮点数加减法</title>
<meta name="author" content="xw5" />
<style type="text/css">
a{padding:0 10px;line-height:24px;text-decoration:none;}
</style>
</head>
<body>
<input type="text" >
<input type="text" >
<a href="javascript:void(0);">+</a>
<a href="javascript:void(0);">-</a>
<script type="text/javascript">
window.onload=function(){
var inputa=document.getElementsByTagName("input"),
linka=document.getElementsByTagName("a");
linka[0].onclick=function(){
console.log(sumfloat(inputa[0].value,inputa[1].value,"+"));
}
linka[1].onclick=function(){
console.log(sumfloat(inputa[0].value,inputa[1].value,"-"));
}
//浮点数加减法
function sumfloat(num0,num1,bzstr){
var ln0,//第一个值的的小数位数
ln1,//第二个值的的小数位数
lnz,//小数位数的最大值
lncz,//小数的差数
num0str,//第一个值数字转字符
num1str,//第二个值数字转字符
resultz;//计算结果
try {
ln0=num0.toString().split(".")[1].length;//获取小数位数
}catch(e){
ln0=0;
}
try {
ln1=num1.toString().split(".")[1].length;//获取小数位数
}catch(e){
ln1=0;
}
lnz=Math.max(ln0,ln1);//取得小数位数的最大值
lncz=ln0-ln1;
//如果数字原本就是整形,直接执行计算
if(lnz===0){
if(bzstr==="+"){
resultz=Number(num0)+Number(num1);
}else{
resultz=Number(num0)-Number(num1);
}
return resultz;
}
num0str=clearpoint(num0,".");
num1str=clearpoint(num1,".");
//根据lncz的正负来判断哪个数字的位数是短的,来走补全
if(lncz>0){
num1str=getbq(num1str,lncz);
}else if(lncz<0){
num0str=getbq(num0str,Math.abs(lncz));
}
//根据传入的符号来判断是做加法还是减法运算
if(bzstr==="+"){
resultz=(Number(num0str)+Number(num1str)).toString();
}else{
resultz=(Number(num0str)-Number(num1str)).toString();
}
//return resultz;
return Number(resultz.slice(0,-lnz)+"."+resultz.slice(-lnz));
}
//补全0
function getbq(str,len){
for(var i=0;i<len;i++){
str=str+"0";
}
return str;
}
//浮点型数去小数点转字符串
function clearpoint(num,str){
return num.toString().replace(str,"");
}
}
</script>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |