Rounding to two digit in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jay123
    New Member
    • Sep 2008
    • 121

    Rounding to two digit in javascript

    Hello,
    This seems to be very common question, but my problem hasn't been addressed anywhere.

    My code look like
    Code:
    var A = Math.round(B * Math.pow(10,2))/Math.pow(10,2);
    Now the code works absolutely fine if B is 1.2345.It gets changed to 1.23 (2 digits, hunky dory)

    problem starts if B is 1 , I need to display 1.00 instead of 1 as i am assigning this variable A value to some textbox and just for the sake of consistency we need to show every thing upto two decimal places.

    I have googled for 2 hours and everyone shows how to round a complex decimal number to 2 decimal points but i haven't come across any webpage showing the solution for my problem.

    Any help will be appreciated.

    Jay
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    er, B.toFixed(2) or B.toPrecision(2 )?

    Comment

    • jay123
      New Member
      • Sep 2008
      • 121

      #3
      didnt work for my problem Dormilich, ok my whole problem is:

      I have a page with a number(A) displayed as label ,Textbox(B) and Button

      When one click on button it adds A and B and sum is displayed in textbox(C). Now thats where i want to display data to 2 digit . I tried your toPrecision(2) and in Textbox C it shows 2.3e+3, instead of 2318 (where A(Label) was 2317.28 and i entered in .72 in B)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        it should work then with .toFixed(2)

        Comment

        • jay123
          New Member
          • Sep 2008
          • 121

          #5
          Worked like a charm, Thanks

          Comment

          Working...