Results 1 to 4 of 4
  1. #1
    andy3 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default formatting numbers in output

    i'm using Formatter class.

    int x = 2010;
    System.out.format("Result: %d", x);

    how do i remove first two integers from x?
    to have the following output // -> "10"

    i have read Formatter and i understood how to perform "a right shift", have a zero-padded result and so on, but could not find anything on how to remove integers from the left side of the result.:confused:

    please, help.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,385
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by andy3 View Post
    i'm using Formatter class.

    int x = 2010;
    System.out.format("Result: %d", x);

    how do i remove first two integers from x?
    to have the following output // -> "10"

    i have read Formatter and i understood how to perform "a right shift", have a zero-padded result and so on, but could not find anything on how to remove integers from the left side of the result.:confused:
    I'd do this:

    Java Code:
    int x= 2010;
    int lastTwo= x%100;
    kind regards,

    Jos

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Formatter is intended to format output, not change its value.

    An alternative way to JosAH's suggestion is to trim off the two leftmost characters after formatting:
    Java Code:
    System.out.print("Result: "+ String.format("%d", x).substring(2));

  4. #4
    andy3 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Formatting numbers or decimals (around comma)
    By Joris in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2010, 05:46 PM
  2. Formatting output somewhat like a table
    By latereg in forum New To Java
    Replies: 3
    Last Post: 04-06-2010, 06:44 AM
  3. Need help formatting File Output
    By aaroncarpet in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 05:26 PM
  4. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 04:58 AM
  5. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:25 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
  •