This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JSF and the fly likes JSF round value from string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF round value from string" Watch "JSF round value from string" New topic
Author

JSF round value from string

Mark Doerbandt
Greenhorn

Joined: Jun 30, 2011
Posts: 8

Hi, everybody,

I have a JSF 1.2 view which is kind of a report generator.
Users can select a query, configure some values, then the database is requested and data collected.
After that the data can be viewed in a dataTable and finally is exported to Excel.
Works fine so far.

Depending on the query the columns have different types, one of which is Double.
The Double values should be rounded now...

Since there are many rows (tens or hundreds of thousands) I collect all data as String
to avoid time consuming type conversions.
That's pretty ok because for the export to Excel I need Strings, too (written to Office XML).

But now the rounding comes in. In the dataTable the doubles shall be rounded.
If I had a Double I could easily use f:convertNumber, but I don't have.

My idea would be a String converter which analyzes if it is a numeric value
and then formats the String. (Performace is not so critical because there are
only 30 or so records in the paged dataTable). How would I accomplish this?

Any (other) suggestions? Thanks!
Mark
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

I think that whatever minor performance benefits you got by not using binary numeric objects it going to be more than negated here.

The easiest way for a converter to add decimal places is to scan the object using a Regular Expression parser and if it's all digits ("^\\d+$" or "^\\d+\\.\\d+$"), convert it to a number and use the formatnumber function.

If you want to avoid converting the number, you can do the Regular Expression scan and follow it up with hammering on the original string object until you've produced one with the requisite number of decimals.

However, either of these methods is more work - and more resources - than simply working with binary objects to begin with.


Customer surveys are for companies who didn't pay proper attention to begin with.
Mark Doerbandt
Greenhorn

Joined: Jun 30, 2011
Posts: 8

Thanks, Tim,
... that's why I asked. I'm not really sure which way to go.
I'm considering using the right types when collecting the data as well. Do it right in the first place.
For now I'll give the converter a try (need a solution next days) and for the next release
I will go for a cleaner solution with typed data. (This has some implications for the export as well.)
Mark
Mark Doerbandt
Greenhorn

Joined: Jun 30, 2011
Posts: 8

solved for now with
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665
    
  15

That works too. Exceptions resource users, although without running real-world benchmarks I can't say which one costs more - Regular Expression parsing or your approach. As they say, Your Mileage May Vary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF round value from string
 
Similar Threads
Doubt on '#' in query.
Fixed Header on Tables?
Displaying 2D array using data tables
How to check if String() value is numeric