asp.net C # int type conversions

Source: Internet
Author: User

ASP tutorials. NET C # int type conversions

int i =-1; BOOL B = Int.tryparse (null, out i);

After execution, b equals false,i equals 0, not equal to 1, remember.

int i =-1; BOOL B = Int.tryparse ("123", out I);

After execution, b equals true,i equals 123;

1, (int) is a type conversion, when we type to the Long,float,double,decimal type, you can use implicit conversion, but when we need to use explicit conversion from a long type to an int type, a compilation error will result. 2, Int.parse () is a class-capacity conversion, which indicates that a string of numeric content is converted to an int type.

Throws a ArgumentNullException exception if the string is empty;

Throws a FormatException exception if the string content is not a number;

Throws a OverflowException exception if the string content represents a number that exceeds the range that the int type can represent;

3, Int.tryparse and Int.parse are more similar, but it will not produce anomalies,

Conversion succeeded returning true, and conversion failed to return false.

The last parameter is an output value, and if the conversion fails, the output value is 0, and if the conversion succeeds, the output value is the converted int value

4, Convert.ToInt32 () is a class-volume conversion, but it is not limited to converting a string to an int type, but also to other types of parameters;

Comparison: When the Convert.ToInt32 parameter is null, returns 0, and an exception is thrown when the Int.parse argument is null. When the Convert.ToInt32 parameter is "", an exception is thrown, and an exception is thrown when the Int.parse parameter is "". Convert.ToInt32 more types can be converted; Int.parse can only convert strings of numeric types

Convert.ToInt32 to cast, but found that when the string value is decimal, the scientific notation cannot be converted. This is not the case if the string was converted to Detimal before ToInt32.

public static int Do (string value)
{
if (String.IsNullOrEmpty (value)) return 0;

decimal result = 0;
try {decimal.tryparse (value, out result);}
Catch {}
return System.convert.toint32 (Result);
}

My Conversion class all code

Namespace Cs.convert
{
public class ToInt
{
public static int Do (string value)
{
if (String.IsNullOrEmpty (value)) return 0;

decimal result = 0;
try {decimal.tryparse (value, out result);}
Catch {}
return System.convert.toint32 (Result);
}

public static int do (decimal value)
{
return System.convert.toint32 (value);
}

public static int do (float value)
{
return System.convert.toint32 (value);
}

public static int do (double value)
{
return System.convert.toint32 (value);
}

        public static int do (int value)
         {
            return value;
        }

        public static int Do (object value)
         {
            if (null = value | | String.IsNullOrEmpty (Value.tostring ()) return 0;
            Decimal result = 0;
            try {decimal.tryparse (value.tostring ()), Out result);
            Catch {}
             return System.convert.toint32 (result);
       }

        public static int do (DBNull value)
         {
            return 0;
        }
   }

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.