Results 1 to 6 of 6
Thread: truncate a decimal number ?
- 10-13-2010, 04:57 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
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.
-
- 10-13-2010, 05:53 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 10-13-2010, 07:51 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
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
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 downJava Code:System.out.printf( "%7.5f %n", (annualInterestRate/365)); System.out.print("%");
How can I solve this?Java Code:Daily Interest Rate: 0.00027 %
- 10-13-2010, 08:21 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.)
- 10-13-2010, 08:58 PM #6
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Converting whole number into decimal
By jim01 in forum New To JavaReplies: 2Last Post: 09-23-2010, 07:58 PM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
Store the decimal number into an variable
By fataguila in forum New To JavaReplies: 4Last Post: 02-01-2010, 07:22 PM -
Converts a binary number to a decimal
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 09:57 AM -
To truncate jump of line in jsp
By Eric in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-09-2007, 03:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks