仅当字符串超过12个时,如何使用toFixed()
方法?否则,我希望它能正常显示,所以它就像一个普通的计算器应用程序。我已经使用了最大字符法,但它看起来不好,不应该使用它。
$(document).ready(function(){
var inputs=[""];
var totalString;
var operators1=["+", "-", "/", "*"];
//opertors array with the . for validation
var operators2=["."];
var nums = [0,1,2,3,4,5,6,7,8,9];
function getValue(input){
if(operators2.includes(inputs[inputs.length-1])===true && input==="."){
console.log("Duplicate '.'");
}
else if(inputs.length===1 && operators1.includes(input)===false)
{
inputs.push(input);
}
else if(operators1.includes(inputs[inputs.length-1])===false){
inputs.push(input);
}
else if(nums.includes(Number(input))){
inputs.push(input);
}
update();
}
function update(){
totalString = inputs.join("");
$("#steps").html(totalString);
console.log(inputs);
}
function getTotal(){
totalString = inputs.join("");
$("#steps").html(eval(totalString));
}
$("a").on("click", function(){
if(this.id==="deleteAll"){
inputs=[""];
update();
}
else if(this.id==="backOne"){
inputs.pop();
update();
}
else if(this.id==="total"){
getTotal();
}
else{
if(inputs[inputs.length-1].indexOf("+","-","/","*")===-1)
{
getValue(this.id);
}
else
{
getValue(this.id);
}
}
});
});
发布于 2016-11-16 19:41:04
如果你真的必须使用toFixed
,那么它可能是这样的:
let string = "333453453453453453";
string.length > 12 && (+string).toFixed();
https://stackoverflow.com/questions/40630580
复制相似问题