lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Ludwig <>
Subject Re: BigDecimal values
Date Thu, 20 Nov 2008 20:05:35 GMT
Sergey Kabashnyuk schrieb:
> Unfortunately, I have to index any possible number of
> java.math.BigDecimal

Hi Sergey,

quite a lot of numbers are possible for BigDecimal. Somehow the range
must be bounded.

Let's first draw the line where, for a given BigDecimal bd, the result
of bd.toString(), which since 1.5 returns a "standard canonical string
form", cannot be refed to the String constructor for BigDecimal. So when
reconstruction fails, that is out of range for you.

### 9.999E2147483647 still works
9.999E+2147483647 - toString()
99.99E+2147483646 - toEngineeringString()
Rekonstruktion via toString():            works
Rekonstruktion via toEngineeringString(): works

### 10.001E2147483647 too big, does not work
1.0001E+2147483648 - toString()
100.01E+2147483646 - toEngineeringString()
Rekonstruktion via toString():            NumberFormatException
Rekonstruktion via toEngineeringString(): works

Next, unlimited precision is a problem. Do you need a precision of two
billion digits? Probably not. De facto, precision is constrained by
available memory. So you see you must rephrase your requirement in order
to accomodate real-world conditions.

> I can rephrase my question this way:
> How can I convert java.math.BigDecimal numbers in to string
> for its storing in lexicographical order

I assume what you mean is formatting the number so that the
lexicographical order of any possible sequence of acceptable numbers
is the same as its numerical order.

You must find a canonical representation like the scientific notation
and then tweak it as follows:

* "N" for negative and "P" for positive numbers ("N" sorts before "P")
* fixed-width zero-padded exponent first, like "E0000000003", base 10
* one digit with marker, like "N2"
* fixed-width zero-padded decimals with marker, like "D008000000000"

This is 2008, "PE0000000003N2D008000000000". YMMV, of course.

I hope this helps.

Michael Ludwig

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message