2

I am aware of the various posts floating out there with regards to the same issue.

Mine its a little bit different and it might be a little obvious, but I will need your comments.

I am currently using Hibernate Search and Lucene to Index entity properties.

I have a bunch of Double properties on my entities.

These entities using the default Bridges from Lucene (Bridge i.e the one in charge converting LongToString and StringToLong) are giving me troubles once the scientific notation starts to be used.

I am trying to show on DataTables on a .xhtml Credit and Debit amounts, their lenght can be as long as 18 digits, and their DataBase (DB2) type is BIGINT.

  1. I can not change the DataBase type to Long for example.
  2. I can not change either the Double type attributes of my entities either to for example Long

So whats the question? Is there a way from a String say "1234567890" to retrieve a Double whose format is 1234567890 and not 1.23456789E9 as it is being done by default by Double.parseDouble(FormattedString)?

PD: I am aware of the existance of DecimalFormat, however take into account using this formater will give me a String formated correctly say : "#######.E0" but what I really need is a Double with such format, however when doing Double.parseDouble(FormattedString) I will loose such format.

Hope I was clear and thanks for any help.

Sanne
  • 6,027
  • 19
  • 34
camiloqp
  • 1,140
  • 5
  • 18
  • 34

3 Answers3

6

Is there a way from a String say "1234567890" to retrieve a Double whose value is 1234567890 and not 1.23456789E9 as it is being done by default by Double.parseDouble(FormattedString)?

Your question doesn't really make sense. 1234567890 is the same value as 1.23456789E9 and a double represents one of them, if and only if it also represents the other.

I am aware of the existance of DecimalFormat, however take into account using this formater will give me a String formated correctly say : "#######.E0" but what I really need is a Double with such format, however when doing Double.parseDouble(FormatedString) I will loose such format.

No, there is no way to construct a Double so that it is displayed in a certain way. The toString method for Double is what it is, and it can't be changed.

The only thing you can do is to for instance use DecimalFormat or String.format but as you've noted, you'll always end up with a String.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I hope you meant `parseDouble` instead of `toString`? So what you really mean is that `parseDouble` is what it is and can't be changed?? – camiloqp Feb 16 '11 at 15:32
  • No, I meant [`toString`](http://download.oracle.com/javase/6/docs/api/java/lang/Double.html#toString%28%29) (the method that in most circumstances defines how a double is printed). – aioobe Feb 16 '11 at 15:35
  • Yeah my bad, I thought it was somehow possible to alter the format of a double number but it isnt possible as you are stating. A solution provided by a colleague was to apply the _formatter_ property on the `` on my .xhtml Additionally I edited the first part of the question to get it clearer. Can you please confirm if a Double type property can not get its format changed? – camiloqp Feb 16 '11 at 15:40
  • You cannot change the default format of `double`, however you can write your own code to produce a `String` from a `double` or a `double` from a `String` any way you wish. – Peter Lawrey Feb 16 '11 at 17:10
4

Don't know nothing of Lucene, but you can never have a Double in a .xhtml Document it is always a characterstring. A Double doesn't have a Format, only a String representation of a Double has.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Well you are right, I forgot to mention how these doubles are being shown. I have a iterating over a list of entities who have amongst different properties, the Double type properties I am mentioning here. Therefore disregard the .xhtml part and check my real question **So whats the question** Thanks – camiloqp Feb 16 '11 at 15:24
1

So I finally got the solution to my problem. After rounding up what aioobe and Jens Schauder said. I am able to format the text dynamically on my .xhtml with the following tag:

<h:outputText value="#{recordTable[column.property]}"
              rendered="#{column.header ne 'Details' and
                          column.header eq ('Total Credit Amount' or
                                            'Total Debit Amount')}">
              <f:convertNumber pattern="########"/>
</h:outputText>

Thanks for making clear to me these basic stuff I had blurred :)

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
camiloqp
  • 1,140
  • 5
  • 18
  • 34