einpoklum
einpoklum

Reputation: 131646

Using ∞ instead of "infinity" in toString()

How can I make Java use the ∞ symbol instead of the string "infinity" when printing/toString'ing floating-point values (float, double, Float, Double)?

Upvotes: 3

Views: 4418

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382170

You can't change the default toString method, which isn't for user display but for debug/logging.

But you may configure your own DecimalFormat :

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);
symbols.setInfinity("∞");
DecimalFormat decimalFormat = new DecimalFormat("#.#####", symbols);

You use it like this :

String str = decimalFormat.format(myDouble);

Note that the formater will automatically add a minus sign in case of negative infinity.

Upvotes: 9

Related Questions