-1
   <script>
     function e(element)
     {
        return document.getElementById(element);
     }

     function f(element)
     {
        return parseFloat(e(element).value);
     }

     function updateResult()
     {

        e("inputK").value = f("inputG")*1.33* (f("inputI")/(f("inputH")+ f("inputI")))
     }

  </script>

I request help in rounding the values to 2 decimal place for the code displayed above

result-- input type="text" id="inputK" readonly="true" i have tried math.round(inputK) it does not work your suggestion will help me in improving the result.

Sathya
  • 3
  • 5
  • There is no Java here. – CertainPerformance Jul 12 '18 at 05:02
  • possibly duplicate to https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary – Terry Wei Jul 12 '18 at 05:05
  • 1
    Possible duplicate of [Round to at most 2 decimal places (only if necessary)](https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary) – Jon P Jul 12 '18 at 05:09
  • Welcome to StackOverflow, please take the [tour] and read the [help]. At StackOverflow, it is preferred that you put effort into solving your question first. There are many answers to this exact question on the internet including this website. – Jon P Jul 12 '18 at 05:14

2 Answers2

0

As mentioned by you are using Math.round() which returns integer only. There are multiple ways to achieve the goal.

  1. Math.round(num * 100) / 100 -- returns number
  2. var numb = 123.23454; numb = numb.toFixed(2); -- returns string
  3. Math.round(num + "e+2") + "e-2" -- returns number
  4. Number.prototype.round = function(places) { return +(Math.round(this + "e+" + places) + "e-" + places); } -- returns number
Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28
0
e("inputK").value= f("inputK").toFixed(2);

the following code helps in getting me the solution for two decimal place thank you

Sathya
  • 3
  • 5