java - How to extract value from a formatted price? -
i regex in java extracts value price. example, $40,250.99 expression output 40250.99. key thing has done regular expressions. cannot string concatenation or other string related manipulations. have tried following "," messes everything. $503.34 yield 503.34 $40,250.99 yields 40
string extractionpattern = "[\\$](\\d+(?:\\.\\d{1,2})?)"; string val = " executive billings @ $40,250.99 starting 2013-01-05 bi weekly"; pattern p = pattern.compile(extractionpattern); matcher m = p.matcher(val); if (m.find()) system.out.println("found match:" + m.group(1));
there better ways , i'll first admit regex pretty basic...
string regexp = "\\$[0-9,\\.]+"; string value = "executive billings @ $40,250.99 starting 2013-01-05 bi weekly"; pattern pattern = pattern.compile(regexp); matcher matcher = pattern.matcher(value); // should result in list 1 element of "$40,250.99" list<string> lstmatches = new arraylist<string>(5); while (matcher.find()) { lstmatches.add(matcher.group()); } (string match : lstmatches) { // strip off unrqeuired elements... match = match.replaceall("\\$", ""); match = match.replaceall(",", ""); system.out.println(double.parsedouble(match)); }
you may able use numberformat.getcurrencyinstance().parse(match)
parse result double
, assume input matches local requirements...
Comments
Post a Comment