-2

When I am trying to calculate the values My output has 15 digits in decimal values Like if x=3 Then in output it is showing 5.196152422706632

But how can I limit it to 5.19615

How to limit decimal digits in output from 15 digits to 5 digits in JavaScript? Here is my script:

   <script>
function myFunction() {
    var x = document.getElementById("phase").value;

    document.getElementById("demo").innerHTML =  "<b>V<sub>L</sub>is</b><br>" + Math.sqrt(3)*x + "volts";
}
</script>

How can I use this:

double number = 0.#############;
DecimalFormat numberFormat = new DecimalFormat("#.#####");
Aurora0001
  • 13,139
  • 5
  • 50
  • 53
Temp
  • 1
  • 1
  • 3
    java and javascript are two completely different languages – Janar Nov 11 '17 at 15:38
  • Please make sure that you provide detailed explanation on what the problem is. Also, if there are any error messages, provide them as well. hm, and this is not Java, this is JavaScript. – Blejzer Nov 11 '17 at 15:38

3 Answers3

0

The toFixed method allows you to set the number of digits.

I would use it like this:

document.getElementById("demo").innerHTML =  "<b>V<sub>L</sub>is</b><br>" + (Math.sqrt(3) * x).toFixed(5) + "volts";

Btw, java is a completely different language to javascript - you're not using it here

Billy Reilly
  • 1,422
  • 11
  • 11
0

In javascript you can fix the no of digits you want to display after decimal by using the function - toFixed(n).

Here n specifies the no of digits to display after decimal.

<script>

function myFunction() {
    var x = document.getElementById("phase").value;    
    document.getElementById("demo").innerHTML =  "<b>V<sub>L</sub>is</b><br>" + (Math.sqrt(3)*x).toFixed(5) + "volts";
}
</script>
-2

In java you can do it like this.

public static void main(String[] args)
{
    String value = String.format("%.3f", Math.sqrt(3)*9);
    System.out.println("Value with 3 decimals: " + value);
}

In javascript you should check this anwser.

M0nst3R
  • 5,186
  • 1
  • 23
  • 36
maarkeez
  • 98
  • 1
  • 3