如何修复此语法错误?
代码是:
function roundAmount(theDecimal) {
var s = "" + Math.round(theDecimal * 100) / 100
var i = s.indexOf('.')
if (i < 0) {
return s + ".00"
}
var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
if (i + 2 == s.length)
t += "0"
return t
}
出现错误的行:
if (i < 0) return s + ".00"
错误是:
error: expected (;)
有谁知道如何解决这个问题?
The code is:
function roundAmount(theDecimal) {
var s = "" + Math.round(theDecimal * 100) / 100
var i = s.indexOf('.')
if (i < 0) {
return s + ".00"
}
var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
if (i + 2 == s.length)
t += "0"
return t
}
The line with the error:
if (i < 0) return s + ".00"
The error is:
error: expected (;)
does anyone know how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于您的脚本:
上面脚本中的问题是最后一个 if 语句执行一些操作,然后返回。 操作后需要一个分号。
将来,作为良好的做法,请确保在每个有效语句后添加分号。 这样就不会打扰你了。
将每一行视为一个想法,并将大括号视为将想法“分组”和“关联”在一起的方式。
下面是一个完整的想法,说“给我一个变量“i”,并给它值 1 + 2;
下面是一个关于条件的完整想法,说“如果 i 是 3,则将 1 添加到 i”。 “add 1 to i”是它自己的想法,所以它需要一个分号,因为 IF 语句的大括号很特殊,因为它们在“完整的想法”之后不需要分号,只要你放一个“。块”(这就是大括号真正的作用),将思想括起来。
这意味着以下内容是有效的:
以下内容无效有效,因为 if 后面的分号结束了“思想”在 if 之前知道如果 i 等于 3 该怎么办:
有关基本 JavaScript 教程,请查看 W3Schools。
“一定有更好的方法吗?”
每当您发现自己对小数进行大量字符串操作时,最好问问自己“有一个更好的方法吗?”。
看起来您正在编写一个函数,将数字四舍五入到最接近的百分之一,同时显示两位小数点。 有一种更简单的方法可以做到这一点。 您可以四舍五入到最接近的百分之,然后让 javascript 输出定点数。
例子:
About your script:
The problem in the script above is that last if statement which does some operations followed by a return. You need a semi-colon after the operation.
In the future, as good practice, make sure to put a semi-colon after every valid statement. That way this won't bother you.
Think of each line as a thought, and curly braces as ways to "group" and "relate" thoughts together.
The below is a full thought that says "give me a variable "i" and give it the value 1 + 2;
The below is a full thought about a condition that says "If i is 3 then add 1 to i". The thought "add 1 to i" is its own thought, so it needs a semicolon. Since the curlybraces for the IF statement are special in that they don't need a semi-colon after their "full thought" as long as you put a "block" (which is what curlybraces really make) after it, to enclose the thought.
This means the following is valid:
The following is not valid because the semi-colon after the if ends the "thought" before the if knows what to do if i equals 3:
For a basic JavaScript tutorial, check out W3Schools.
"There must be a better way?"
Any time you find yourself doing a lot of string operations on decmials, it's a good idea to ask yourself "is there a better way to do this?".
It looks like you're writing a function to round a number to the nearest hundredths while displaying two decimal points. There's a much easier way to do this. You can just round to the nearest hundredths and have javascript output the fixed point number.
Example:
请注意语句末尾的
;
。 就我个人而言,我更喜欢将几乎所有if
放在{}
中,例如“Which有助于调试和单步执行代码”。
Note the
;
at the end of the statement. Personally, I prefer surrounding almost all myif
s in{}
such asWhich helps in debugging and stepping though code.
它需要一个分号,所以添加一个分号。
It's expecting a semicolon, so add a semicolon.
在行尾添加分号! 像这样:
Add a semicolon at the end of the line! Like this:
我不认为您在之前的评论中发布的代码有任何特别的错误,但我建议您始终在语句末尾添加分号。 尽管它们不是必需的,但通常可以更轻松地调试此类问题。
I don't see anything particularly wrong with the code you posted in the earlier comments, but may I suggest always adding a semi-colon to the end of your statements. Though they are not required, it usually makes it easier to debug problems such as these.