vb.net - Double.Parse vs Double.TryParse -


I came across, I did not understand what is using TryParse. I am creating a format converter. I have this code in a very standard format converter converback function

  public function convertback (value as object, type of target type, parameter as object, as globalization) Culture.Cultulture.info) IvalueConverter.ConvertBack as Values ​​Component, "Values ​​here" is "$ 1495,00" and culture is "N-US" Val = Double. Purse (value. Toasting, globalization number, styling. Loans) if double. Tri-Pars (value to string, val) end if return 0 and function  

But it's strange about it, double. Give me a wrong answer to the tray purse, and double. Convert purses, values ​​correctly

Why Double TRAI PERSES, value can not be changed ??

  repeat Trips ([convert to string], out of results);  

TryParse function which accepts two settings, is the first string that you want to change and the output parameter with the second result value, but its method True or false return based on conversion If the conversion has been successfully completed then this truth will not be returned, if it does not go wrong

  Double. Purse ([string to convert]); The  

parse function does not return true or false based on conversion, if this is tried successfully, then this value will change, otherwise it will change Exception will throw if you want to use the parse function, then to avoid it try and capture exception P << p> UPDATE The problem you face in string format

Rica that is most closely related to the code provided by you

  Public static decimal parse (string input) {return decimals. Change purse (regex. (Input, @ "[^.]", "")); }  

This is an option which will support negative numbers, and if it gets a second duration value, it will reduce the number of strings that are not valid decimal values. Some other modifications have not been seen in OP, which can handle additional cases in your current code.

  public static decimal purse (string input) {return decimal.Parse (Regex.Match (input, @ "-? \ {{{{{{{1}} (*, \ d {3}) * (\. \ D +)? ").););); }  

Comments