1. Solve the conversion problems caused by different regions and time on different computers (e.g. "123.456" report "Incorrect string format" issue )
/// <summary> ///Math Conversion Class/// </summary> Public classMathconverter {/// <summary> ///Object Convert float (conversion failed, attempt to convert first part number to float)/// </summary> /// <param name= "Obj2float" ></param> /// <returns>Default: 0.00</returns> Public Static floatObjecttofloat (Objectobj2float) { floatresult =0.00f;//Default Value if(Obj2float! =NULL) { stringStr2float = Obj2float.tostring ();//object to String if(!float. TryParse (Str2float, outResult))//string is converted directly to float, and if it fails, gets the first part of the string converted to float { stringStrnumber =string. Empty; foreach(CharIchrinchstr2float) { if(Char.isnumber (ICHR)) {Strnumber+=ICHR; } Else { Break; } } if(!string. IsNullOrEmpty (Strnumber)) {float. TryParse (Str2float, outresult); } } } returnresult; } /// <summary> ///Object Convert float (conversion failed, attempt to convert first part number to float)/// </summary> /// <param name= "Obj2float" ></param> /// <param name= "Result" >Default: 0.00f</param> /// <returns>false: Conversion failed</returns> Public Static BOOLTryobjecttofloat (ObjectObj2float, out floatresult) { BOOLIssuccess =false; Result=0.00f;//Default Value if(Obj2float! =NULL) { stringStr2float = Obj2float.tostring ();//object to String if(!float. TryParse (Str2float, outResult))//string is converted directly to float, and if it fails, gets the first part of the string converted to float { stringStrnumber =string. Empty; foreach(CharIchrinchstr2float) { if(Char.isnumber (ICHR)) {Strnumber+=ICHR; } Else { Break; } } if(!string. IsNullOrEmpty (Strnumber)) {if(float. TryParse (Str2float, outresult)) {issuccess=true; } } } Else{issuccess=true; } } returnissuccess; } }View Code
C # string converted to float