• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
msxgupta Gupta wrote:

Please check your private messages for an important administrative matter
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!