c# - Convert string value with '$' into double -
have not found similar question on yet (regarding '$').
i'm following this example, trying expand pre-existing method convert strings doubles. general structure of code looks so:
string str = "$1,000.00"; double output; var success = double.tryparse( str , numberstyles.allowleadingwhite | numberstyles.allowtrailingwhite | numberstyles.allowleadingsign | numberstyles.allowdecimalpoint | numberstyles.allowthousands | numberstyles.allowexponent | numberstyles.allowcurrencysymbol , null // cultureinfo.invariantculture // or numberformatinfo.invariantinfo break conversion '$'.. , out output ); console.writeline("'{0}' --> {1}, {2}", str, output, success);
this works. problem i'm getting dealing '$' character when theiformatprovider
parameter of double.tryparse()
method set either cultureinfo.invariantculture
or numberformatinfo.invariantinfo
(i.e. replace null
in method call 1 of commented out variables)
why that? need set parameter 1 of suit internationalization of input strings? initial implemenation of method had parameter set numberformatinfo.invariantinfo
breaks conversion of string contains '$'.
why that?
the currency symbol invariant culture ¤
, not dollar sign. that's why fails.
however, not need modify conversion code have account - can modify string pass in.
static string replacecurrency(string str) { return regex.replace(str, @"\p{sc}", "¤"); }
this replace currency symbol invariant symbol - parse correctly.
string str = "$1,000.00"; double output; var success = double.tryparse(replacecurrency(str), numberstyles.currency, cultureinfo.invariantculture, out output); console.writeline(output);
output
1000
Comments
Post a Comment