2016-11-16 7 views
0

toFixed()メソッドは、文字列が12を超える場合にのみ使用しますか?さもなければ私はそれを正常に表示したいので、普通の電卓アプリのようです。私は最大の文字メソッドを使用していますが、見栄えが良くないので使用しないでください。固定メソッドへのJavaScript

enter image description here

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

}); 
+1

あなたもtoFixed、例えばを使用していない気でした。.. '(1234567890.123)。サブストリング(0,12) ''。'がある場合は、余分なチェックをするだけでよいでしょう。終わりに限り、あなたはそのアイデアを得るべきです。 – Keith

+0

@Keithありがとう、たくさんの男。出来た! – EyedFox1

答えて

0

あなたもし本当には、それはそうのような可能性がtoFixedを使用する必要があります。

let string = "333453453453453453"; 
string.length > 12 && (+string).toFixed(); 
関連する問題