• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

numberformat.parse Vs str.replaceall ... which one is preferred?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

Can you please advice me that which option is more preferred in this particular problem:

we have to remove comma (,) from the string(always conatins a number) before convert it into BigDecimal
option 1: use replaceall() i.e.
str2 = str1.replaceall(",","") ;
BigD = new BinDecimal(str2);

option 2: use numberformat
BigD =new BigDecimal(numberFormat.parse(str1).floatValue());

As far as the requriment concern --> it's a big enterprise application... str1 contain data of finincial range.

please ignore lexical error.

Thanks a lot for giving your time & concern






 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

msxgupta Gupta wrote:


Please check your private messages for an important administrative matter
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

msxgupta Gupta wrote:option 2: use numberformat
BigD =new BigDecimal(numberFormat.parse(str1).floatValue());


And what if you deploy your application to a machine with a different locale, where not comma but dot is the decimal grouping symbol? Your code will be broken. I would definitely go for the first option. However, I would probably use a simple StringBuilder + loop combination to filter out the commas myself. That way you don't start the regular expression engine which is slower. (I've tested, and the simple StringBuilder + loop combination is 10 times faster over a total of 100,000 iterations.)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic