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:19 PM

Fubarable

printf could do this or DecimalFormat

10-13-2010, 05:53 PM

Tolls

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

hacikho

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 down

Code:

Daily Interest Rate: 0.00027
%

How can I solve this?

10-13-2010, 08:21 PM

pbrockway2

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.

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.