views:

304

answers:

5

DecimalFormat parses Double.toString() representation (which could be in scientific and financial format).

Why Sun has chosen this approach, instead of direct converting double to String?

PS: To be more concrete. Why DecimalFormat internally uses Double.toString() in order to format Double, instead of formatting internal representation of Double itself?

+1  A: 

DecimalFormat gives you more control over the format by allowing you to give it a formatting pattern.

Journeyman Programmer
Agree with you, but here I am asking about the way DecimalFormat doing formatting internally.
Andrey Vityuk
A: 

may be he ask like "Double.toString(withSomeParameters)" will do the job instead of using DecimalFormat.

i think the reason, could be i18n. in some places you can type 1,800.39 .. while somewhere else you can type 1.800,39

A: 

I'm not shure what you mean by "direct converting", but toString() is always a debug information only! You should not use it to display values in a frontend. Use DecimalFormat for parsing and formatting!

Arne Burmeister
That I was asking about. Why DecimalFormat internally uses Double.toString() result in order to format Double, instead of formatting internal representation of Double itself.
Andrey Vityuk
A: 

For financial applications, use BigDecimal instead, that give you control over rounding modes and precision.

KarlP
Actually, question related to any application which formats double.
Andrey Vityuk
OK, nevermind then...
KarlP
A: 

Perhaps Sun is following DRY principles.

Robert Harvey