简体   繁体   English

如何串联。 (点)编号为 javascript

[英]How to concatenate . (dot) with number in javascript

hi ive created visual calc using html and its looks like this..我使用 html 创建了视觉计算,它看起来像这样..

| 1  | 2  | 3 | 
| 4  | 5  | 6 | 
| 7  | 8  | 9 | 
|    | .  |   |

and ive created function called number() on click on each html element and this is it我在点击每个 html 元素时创建了名为number()的 function,就是这样

number(number)
{
    this.total_quantity_discount_price = this.total_quantity_discount_price+''+number;
    this.total_quantity_discount_price = parseFloat(this.total_quantity_discount_price);
},

with numbers 0123456789 everything is working fine but my problem with the .数字0123456789一切正常,但我的. how can i add .我该如何添加. to this.total_quantity_discount_price i mean how can i add the number 10.555 or 55.648 ect.. thanks..this.total_quantity_discount_price我的意思是如何添加数字 10.555 或 55.648 等.. 谢谢..

Use Number constructor like使用Number构造函数,如

this.total_quantity_discount_price = Number(this.total_quantity_discount_price+''+number);

 let n=Number(4+'.'+5) // the n is a number which you could add it to another console.log(n) console.log(n+1) console.log(n-3)

You can use the + operator like this:您可以像这样使用+运算符:

 const n = '4' + '.' + '3' + '4'; console.log(+n); console.log(+n + 1); console.log(+n - 1);

You function will become something like:你 function 会变成这样:

function number(number){
  this.total_quantity_discount_price = +(this.total_quantity_discount_price + '' + number);
}

You can concat everything together and parse it with new Function()您可以将所有内容连接在一起并使用new Function()解析它

 let result = ""; let calc = document.getElementById("calculation"); let output = document.getElementById("result"); document.querySelectorAll("button").forEach(el => { el.addEventListener("click", ()=> { result += el.innerHTML; output.innerHTML = result; }) }) function render() { let calcResult = interprete(result); output.innerHTML = calcResult; } function getResult() { output.innerHTML = interprete(result); } function clearResult() { output.innerHTML = ""; result = ""; } function back () { result = result.slice(0, -1); output.innerHTML = result; } function interprete(str) { return new Function(`return ${str}`)() }
 .buttonholder { display: flex; flex-flow: wrap; width: 170px; } button { display: block; margin: 2px; padding: 15px; }.box { cursor: pointer; background: black; color: white; padding: 10px; margin: 10px; } #result { background: green; color: white; padding: 10px; }
 <div class="buttonholder"> <button>1</button> <button>2</button> <button>3</button> <button>4</button> <button>5</button> <button>6</button> <button>7</button> <button>8</button> <button>9</button> <button>.</button> <button>+</button> <button>-</button> <button>*</button> <button>/</button> </div> <div class="box" onclick="back()"> <== </div> <p id="result"></p> <div class="box" onclick="getResult()">=</div> <p id="calculation"></p> <div class="box" onclick="clearResult()">Clear</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM