Results 1 to 6 of 6
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default truncate a decimal number ?

    I have a double variable, value of it is 0.8251913258676211, But I want to display only 0.82 . And Later than I am going to use this 0.82 for arithmetic expression so I can not do toString(). Is there anyway to do this and how?.
    I am not trying to round my number;
    I got my number as a result of calculation : 0.8251913258676211
    I want ot display it as 0.82 and I want to use this 0.82 later in another calculation.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    BigDecimal, ROUND_DOWN, scale = 2.
    That's the only way to make sure that you are using 0.82 in further calculations.
    printf will display it as 0.82, but the value will still be 0.8231676576 etc.

  4. #4
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    Thanks for advising printf, I used it and it is working. But I have another question, it might be easy however I could not come up any idea. By using printf in below codes

    Java Code:
    System.out.printf( "%7.5f %n", (annualInterestRate/365));
    System.out.print("%");
    I have below result, But I want to display % right next to the number, like this 0.00027% . I tried string concetenation inside the printf and got error. After printf medhod cursor goes to one line down
    Java Code:
    Daily Interest Rate: 0.00027 
    %
    How can I solve this?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Just as we use %f for displaying floating point numbers, and %n to display a newline, we can also use %% to display a percentage sign.

    Java Code:
    "%7.5f%%%n"

    For a complete description see the Formatter API docs - you wouldn't want memorise these! But it's the place to go back to each time you have sometime a bit different to do with formatting.

    (Originally you said you wanted to *truncate*, %f will round.)

  6. #6
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

Similar Threads

  1. Converting whole number into decimal
    By jim01 in forum New To Java
    Replies: 2
    Last Post: 09-23-2010, 07:58 PM
  2. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  3. Store the decimal number into an variable
    By fataguila in forum New To Java
    Replies: 4
    Last Post: 02-01-2010, 07:22 PM
  4. Converts a binary number to a decimal
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:57 AM
  5. To truncate jump of line in jsp
    By Eric in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-09-2007, 03:58 AM

Posting Permissions

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