0

toFixed()仅当字符串超过 12 个时,我如何使用该方法?否则我希望它正常显示,所以它就像一个普通的计算器应用程序。我使用了最大字符方法,但它看起来不太好,不应该使用。

在此处输入图像描述

$(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);
    }
}
});

});
4

1 回答 1

0

如果你真的必须使用toFixed那么它可能是这样的:

let string = "333453453453453453";
string.length > 12 && (+string).toFixed();
于 2016-11-16T11:41:04.023 回答