Jordan S
Jordan S

Reputation: 29

Rounding a double to two decimal places without using DecimalFormat

I wanted to know is it possible format a double to round to two decimal places with trailing zeros without using DecimalFormat, or importing anything for that matter? I've been trying to think of one for the past half hour and I can't figure it out.

Upvotes: 0

Views: 253

Answers (2)

Absurd-Mind
Absurd-Mind

Reputation: 7994

You can use String.format();

String s = String.format("my double: %.2f", 3.54123);
System.out.println(s);

or directly printf

System.out.printf("my double: %.2f%n", 3.54123);

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240928

How about ?

(((int)(number * 100))/100.0F) 

Example

Upvotes: 0

Related Questions