admin管理员组

文章数量:1244271

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?

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?

Share Improve this question edited Jul 23, 2009 at 17:33 Brandon 70k32 gold badges198 silver badges226 bronze badges asked Jul 23, 2009 at 17:13 problem_bringer121problem_bringer121 9
  • oh by the way the laguage is java script thanks – problem_bringer121 Commented Jul 23, 2009 at 17:15
  • a semi-colon missing shouldn't matter in javascript – Brian Ramsay Commented Jul 23, 2009 at 17:17
  • that line is fine. Error must be somewhere else. I think it might be one line above perhaps. Need to see more code... – Jimmy Chandra Commented Jul 23, 2009 at 17:17
  • 4 Is this the type of question that we should just answer by fixing the question? i.e. "My problem was that I wrote {this}, but I really should have written {that}" where {this} is his original stuff and {that} is the "correct" answer. – Erich Mirabal Commented Jul 23, 2009 at 17:18
  • 1 <script type="text/javascript"> 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 } hope that helps it still e up with the error when i put the semi colon in – problem_bringer121 Commented Jul 23, 2009 at 17:21
 |  Show 4 more ments

5 Answers 5

Reset to default 5

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 );
}
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.

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

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

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

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

I don't see anything particularly wrong with the code you posted in the earlier ments, 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.

本文标签: javascriptHow do I fix this syntax errorStack Overflow