Results 1 to 6 of 6
  1. #1
    Deb
    Deb is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default Formatting in Java

    oihikibkbk
    Last edited by Deb; 02-27-2013 at 05:55 PM.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Formatting in Java

    DecimalFormat can be used to format decimal numbers.
    As for Strings, personally I would use a regex or something. Or, if you just want to chop off the remainder,

    Java Code:
    String str = "0.3235343434";
    int dot = str.indexOf("\\.");
    str = str.substring(dot, dot+2);
    Or if you want to round it, parse that ^ to a double then "round" it using the DecimalFormat.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Formatting in Java

    Also, if you just want to have them rounded on the output, you can do something like this.

    Java Code:
    public class Example {
     public static void main(String [] arg) {
            double d = 3.444933;
            double e = 3.45555;
            System.out.printf("%4.2f  %4.2f\n", d, e); // prints 3.44  3.46
            
      }
    }
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: Formatting in Java

    Please wrap your code in [code] tags [/code] in order to retain the formatting.

    What does "I am not able to run it" mean?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Formatting in Java

    Looks like her editing of the thread got garbled somehow.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,229
    Rep Power
    19

    Default Re: Formatting in Java

    Deb, any future deletion of content will result in a summary ban without further warning.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. java method for number formatting
    By marall80 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 04-19-2011, 06:25 AM
  2. Formatting Strings in Java
    By ladykrimson in forum New To Java
    Replies: 3
    Last Post: 11-03-2010, 08:31 AM
  3. Formatting Java Code
    By DoubleDee in forum Advanced Java
    Replies: 5
    Last Post: 09-18-2010, 10:33 PM
  4. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  5. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 07:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •