如何修复此语法错误?

发布于 2024-07-28 22:06:21 字数 495 浏览 4 评论 0原文

代码是:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清风夜微凉 2024-08-04 22:06:21

关于您的脚本:

上面脚本中的问题是最后一个 if 语句执行一些操作,然后返回。 操作后需要一个分号。

将来,作为良好的做法,请确保在每个有效语句后添加分号。 这样就不会打扰你了。

将每一行视为一个想法,并将大括号视为将想法“分组”和“关联”在一起的方式。

下面是一个完整的想法,说“给我一个变量“i”,并给它值 1 + 2;

var i = 1 + 2;

下面是一个关于条件的完整想法,说“如果 i 是 3,则将 1 添加到 i”。 “add 1 to i”是它自己的想法,所以它需要一个分号,因为 IF 语句的大括号很特殊,因为它们在“完整的想法”之后不需要分号,只要你放一个“。块”(这就是大括号真正的作用),将思想括起来。

这意味着以下内容是有效的:

if( i == 3 ) {
    i = i + 1;
}

以下内容无效有效,因为 if 后面的分号结束了“思想”在 if 之前知道如果 i 等于 3 该怎么办:

if( i == 3 ) ; {
    i = i + 1;
}

有关基本 JavaScript 教程,请查看 W3Schools

“一定有更好的方法吗?”

每当您发现自己对小数进行大量字符串操作时,最好问问自己“有一个更好的方法吗?”。

看起来您正在编写一个函数,将数字四舍五入到最接近的百分之一,同时显示两位小数点。 有一种更简单的方法可以做到这一点。 您可以四舍五入到最接近的百分之,然后让 javascript 输出定点数。

例子:

function roundAmount( theDecimal ) {
    //first round to the nearest hundredth
    //then return the value with two decimal places as a string
    return theDecimal.toFixed( 2 );
}

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;

var i = 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:

if( i == 3 ) {
    i = i + 1;
}

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:

if( i == 3 ) ; {
    i = i + 1;
}

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:

function roundAmount( theDecimal ) {
    //first round to the nearest hundredth
    //then return the value with two decimal places as a string
    return theDecimal.toFixed( 2 );
}
☆獨立☆ 2024-08-04 22:06:21
if (i < 0) return s + ".00";

请注意语句末尾的 ;。 就我个人而言,我更喜欢将几乎所有 if 放在 {} 中,例如“

if (i < 0) 
{
    return s + ".00";
}

Which有助于调试和单步执行代码”。

if (i < 0) return s + ".00";

Note the ; at the end of the statement. Personally, I prefer surrounding almost all my ifs in {} such as

if (i < 0) 
{
    return s + ".00";
}

Which helps in debugging and stepping though code.

内心激荡 2024-08-04 22:06:21

它需要一个分号,所以添加一个分号。

if (i < 0) 
  return s + ".00";

It's expecting a semicolon, so add a semicolon.

if (i < 0) 
  return s + ".00";
因为看清所以看轻 2024-08-04 22:06:21

在行尾添加分号! 像这样:

if (i < 0) return s + ".00";

Add a semicolon at the end of the line! Like this:

if (i < 0) return s + ".00";
拿命拼未来 2024-08-04 22:06:21

我不认为您在之前的评论中发布的代码有任何特别的错误,但我建议您始终在语句末尾添加分号。 尽管它们不是必需的,但通常可以更轻松地调试此类问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文