Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to string a decimal number in Java?

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question How to string a decimal number in Java?

    So I want to string the number 3.1415 and use the substring to extract each part. So the output looks like this:

    3.1
    3.14
    3.141
    3.1415
    3.141
    3.14
    3.1

    import java.util.*;
     
    public class Program31415 
    {
        static Scanner console = new Scanner(System.in);
     
        public static void main(String[] args)
        {
            String number;
     
            number = 3.1415;
     
            while (number == 3.1415) 
            {
     
                System.out.println(number.substring(0,2));
                System.out.println(number.substring(0,3));
                System.out.println(number.substring(0,4));
                System.out.println(number.substring(0,5));
                System.out.println(number.substring(0,4));
                System.out.println(number.substring(0,3));
                System.out.println(number.substring(0,2));                                  
     
             }    
     
        }
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to string a number?

    Integer.toString(3.1415)
    you can use this for pi if you want more acuracy
    Math.PI

    Regards,
    Chris

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to string a number?

    How do I get the program to stop after running after output the results once. What would I add?

    import java.util.*;
     
    public class Program31415 
    {
        static Scanner console = new Scanner(System.in);
     
        public static void main(String[] args)
        {
            int number = 0;
            String strNumber;
     
            strNumber = Double.toString(3.1415);
     
     
            while (number == 0) 
            {
     
                System.out.println(strNumber.substring(0,2));
                System.out.println(strNumber.substring(0,3));
                System.out.println(strNumber.substring(0,4));
                System.out.println(strNumber.substring(0,5));
                System.out.println(strNumber.substring(0,4));
                System.out.println(strNumber.substring(0,3));
                System.out.println(strNumber.substring(0,2));
     
             }    
     
        }
    }

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to string a number?

    get rid of the while loop altogether xD

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to string a number?

    There has to be a way to end it in the loop

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to string a number?

    you dont need the loop at all....

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: how to string a number?

    import java.util.*;
     
    public class Program31415 
    {
        static Scanner console = new Scanner(System.in);
     
        public static void main(String[] args)
        {
            int number = 0;
            String strNumber;
     
            strNumber = Double.toString(3.1415);
     
     
            while (number == 0) 
            {
     
                System.out.println(strNumber.substring(0,2));
                System.out.println(strNumber.substring(0,3));
                System.out.println(strNumber.substring(0,4));
                System.out.println(strNumber.substring(0,5));
                System.out.println(strNumber.substring(0,4));
                System.out.println(strNumber.substring(0,3));
                System.out.println(strNumber.substring(0,2));
     
                [B]break;[/B]
             }    
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    Lizard (May 14th, 2009)

Similar Threads

  1. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 08:33 AM
  2. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM
  3. [SOLVED] Facing java error "cannot be applied to (java.lang.String)"
    By tazjaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2009, 10:19 AM
  4. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM