decimal - In C#, why is this conversion causing an "Input string was not in a correct format." error? -
I have a code that reads a number (but comes in the form of a string) and I convert it to a byte I am trying to
Normally the value is between 0 and 1 (like .25) and the below code works fine, but now I came to a negative value, especially "-1" and Trying to understand why this code is blowing:
Public Static Bite GetByteVal (Detector Detroit, String Caption) {var val = dataRow.GetValue (Caption). Ostring (). Trim (); If (! String.IsNullOrEmpty (val)) {decimal converted val = decimals Purse (val, number style.alveexent | number equals; approbatory decimals) * 100; If (Convertvalle> = 0) {Return (byte) (converted veal); } Other {return (byte) 0; }} Return (Byte) 0; } When "val" variable comes in the form of "-1", I get an exception on this line:
decimal converted val = decimal . Pars (Val, Number Style) Allow, Expand | Number Style, Allow, EmployPoint) * 100; which says:
The input string was not in the correct format
You must throw in the numbers as AllowLeadingSign as well: Decimal Converted Wheel = Decimal Pars (Val, Number Style. Allow Exposure | Numbers Style. PermalinkMickPoint | NumberSystem Approved Listing Sign) * 100;
Comments
Post a Comment