java - NumberFormat.getCurrencyInstance(); Failure -
java - NumberFormat.getCurrencyInstance(); Failure -
i'm trying parse currency string bigdecimal using numberformat.getcurrencyinstance();
the next scenario $1,000.01 pass 1000.01 pass 1,000.01 fail -> needs parsed 1000.01 in order pass. abc fail -> right behavior.
my method
public bigdecimal parseclient(field field, string clientvalue, string message) throws validationexception { if (clientvalue == null) { homecoming null; } numberformat nf = numberformat.getcurrencyinstance(); seek { homecoming new bigdecimal(nf.parse(clientvalue).tostring()); } grab (parseexception ex) { throw new validationexception(message); } }
anybody know of solution work properly?
it brute force, why not check presence of currency characters @ origin of string, , if not there, prepend it?
if ( !clientvalue.startswith( "$" ) ) { clientvalue = "$" + clientvalue; }
if brute forcefulness approach not desired, next approach take note of each format client can come method. then, create many decimcalformat instances necessary handle incoming input formats -- , allow them parsed.
another approach standardize incoming clientvalue single format. done through string manipulation , regex. however, still have issue need know of different ways incoming clientvalue can come method. requirements of import here.
java number-formatting
Comments
Post a Comment