Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Double.POSITIVE_INFINITY to BigDecimal?

Tags:

java

I tried BigDecimal.valueOf(Double.POSITIVE_INFINITY), but kept getting NumberFormatException

Thanks

like image 962
One Two Three Avatar asked Apr 09 '12 21:04

One Two Three


2 Answers

You cannot represent infinity in BigDecimal.

like image 195
Oliver Charlesworth Avatar answered Oct 13 '22 06:10

Oliver Charlesworth


This behavior corresponds to the documented way the valueOf method is supposed to work:

Parameters: val - double to convert to a BigDecimal.

Returns: a BigDecimal whose value is equal to or approximately equal to the value of val.

Throws: NumberFormatException - if val is infinite or NaN.

like image 22
Sergey Kalinichenko Avatar answered Oct 13 '22 07:10

Sergey Kalinichenko