Results 1 to 3 of 3

Thread: how to increase the number of decimal digits in JAVA program output?

  1. #1
    Join Date
    Jan 2012
    Posts
    42

    how to increase the number of decimal digits in JAVA program output?

    I am writing a small code in java about finding a square root of a number! I should have the facility to decide how many decimal numbers will be shown after decimal points! Is there any method by which I can select how many position to show in the answer? I have declared all the variables as double but still it is showing answer up to 15 digits only! I want another method by which I can show “n” number of digits after the decimal point! Thanks!

  2. #2
    Join Date
    May 2009
    Posts
    527

    re: how to increase the number of decimal digits in JAVA program output?

    If you want more than 15 positions then you should opt for string format or use string.format to convert double into string !
    E.g: String s = String.format(“%1.20f”,1/3);
    This will convert the value in to the string value! But I don’t think You will get what that you want because computer will not be able to save the value that will be coming!

  3. #3
    Join Date
    May 2009
    Posts
    539

    re: how to increase the number of decimal digits in JAVA program output?

    I have got the program that shows how to get the value for the “PI” beyond 15 digits! You can re-edit it for your own purpose! This is working properly on my machine! Hope you will like this!

    import java.math.BigDecimal;
    import java.util.Scanner;
    public class Pi {
    public static void main(String args[])
    {
    Scanner input = new Scanner(System.in);
    BigDecimal var1 = new BigDecimal("4");
    BigDecimal var2 = new BigDecimal("4");
    BigDecimal pi = new BigDecimal("0");
    double var3 = 0;
    System.out.print("Please enter the number of calculations the program should perform: ");
    var3 = input.nextDouble();
    for(double i=1;i<=var3;i+=2)
    {
    var1 = var2.subtract(new BigDecimal((4/(2*i+1))));
    System.out.println(var1)
    var2 = var1.add(new BigDecimal((4/(2*i+3))));;
    System.out.println(var2);
    }
    pi = var1.add(var2);
    pi = pi.divide(new BigDecimal("2"));
    System.out.println("Pi aprox. = " + pi);
    }
    }

Similar Threads

  1. How to get a Java Program output in Tabular format
    By Falgu in forum Software Development
    Replies: 3
    Last Post: 18-07-2012, 04:15 PM
  2. Converting a string number into sequence of digits
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 13-03-2010, 04:46 PM
  3. java program to add leading zeros to a number.
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 07:15 PM
  4. How to write java program to find factorial of number?
    By Balamohan in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 10:14 PM
  5. program to check prime number in java
    By Bansi_WADIA in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 11:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,982,351.44602 seconds with 17 queries