gpt4 book ai didi

javascript - 字符串计算的小数

转载 作者:行者123 更新时间:2023-11-28 02:13:24 26 4
gpt4 key购买 nike

我有一个测验程序的 JavaScript 代码,但我不知道如何返回最后一个上下文行的计算结果,只有 1 位小数。有什么建议吗?

EndQuiz=function(){
canvas.removeEventListener('click',ProcessClick,false);
context.drawImage(quizbg, 0,0,550,90,0,0,550,400);
context.font = "20pt Georgia,Arial";
context.fillText("Thanks for finishing. ",20,80);
context.fillText("This is how good you were:",20,120);
context.font = "16pt Georgia,Calibri,Arial";
context.fillText("Correct answers: "+String(rightanswers)+" of "+String(rightanswers+wronganswers),20,180);

context.fillText("Correct: "+String(rightanswers/(wronganswers+rightanswers)*100)+"%",20,240);1

最佳答案

直接来自 W3schools:

将数字转换为字符串,只保留两位小数:

var num = 5.56789;
var n=num.toFixed(2);

n 的结果将是:

5.57

剩下的留给读者作为练习:-)

关于javascript - 字符串计算的小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16749453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com