C # string converted to float

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.