我有以下 Swift 代码:
\nvar yourShare: Double {\n guard let totalRent = Double(totalRent) else { return 0 }\n guard let myMonthlyIncome = Double(myMonthlyIncome) else { return 0 }\n guard let housemateMonthlyIncome = Double(housemateMonthlyIncome) else { return 0 }\n let totalIncome = Double(myMonthlyIncome + housemateMonthlyIncome)\n let percentage = Double(myMonthlyIncome / totalIncome)\n let value = Double(totalRent * percentage)\n\n return Double(round(100*value)/100)\n }\n \n
Run Code Online (Sandbox Code Playgroud)\n然后该值显示为表单的一部分:
\n Section {\n Text("Your share: \xc2\xa3\\(yourShare)")\n }\n
Run Code Online (Sandbox Code Playgroud)\n我是 Swift 新手,我试图确保yourShare
只有 2 位小数,例如 $150.50,但目前它显示为 $150.50000。我尝试将其四舍五入到小数点后两位,Double(round(100*value)/100) …
我有一个基本的文本输入字段,出于数据分析的目的,我想在用户每次编辑该字段中的文本时对其进行跟踪。到目前为止我有这个代码:
var input = document.querySelector("#inputId");
input.oninput = function (e) {
alert("changed");
}
Run Code Online (Sandbox Code Playgroud)
问题是每次用户删除或添加字符时都会触发,而我只想知道他们正在更改文本字段中的文本一次。有没有办法实现这一目标?我是 jQuery 和 JavaScript 的新手,所以任何方向都将不胜感激,谢谢!