asp.net C# int类型转换

asp教程.net c# int类型转换

int i = -1; bool b = int.tryparse(null, out i);

执行完毕后,b等于false,i等于0,而不是等于-1,切记。

int i = -1; bool b = int.tryparse("123", out i);

 执行完毕后,b等于true,i等于123;

1、(int)是一种类型转换;当我们类型到long,float,double,decimal类型,可以使用隐式转换,但是当我们从long类型到int类型就需要使用显式转换,否则会产生编译错误。 2、int.parse()是一种类容转换;表示将数字内容的字符串转为int类型。

 如果字符串为空,则抛出argumentnullexception异常;

如果字符串内容不是数字,则抛出formatexception异常;

 如果字符串内容所表示数字超出int类型可表示的范围,则抛出overflowexception异常;

3、int.tryparse与 int.parse 又较为类似,但它不会产生异常,

转换成功返回 true,转换失败返回 false。

最后一个参数为输出值,如果转换失败,输出值为 0,如果转换成功,输出值为转换后的int值

 4、convert.toint32()是一种类容转换;但它不限于将字符串转为int类型,还可以是其它类型的参数;

 比较:convert.toint32 参数为 null 时,返回 0; int.parse 参数为 null 时,抛出异常。convert.toint32 参数为 "" 时,抛出异常; int.parse 参数为 "" 时,抛出异常。 convert.toint32 可以转换的类型较多; int.parse 只能转换数字类型的字符串

convert.toint32来强制转换,但发现当字符串值以小数,科学计数法表示时候无法转换。如果在toint32之前先将字符串转换为detimal就不会有这种情况。

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);
        }

我的转换类全部代码

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;
        }
    }
}

时间: 2024-09-20 14:27:19

asp.net C# int类型转换的相关文章

jquery把int类型转换成字符串类型的方法_jquery

jQuery中把获取的number类型数据转换成字符串类型 var val=$("#id).val(); If(typeof val=="number"){ val+=' '; } 以上就是小编为大家带来的jquery把int类型转换成字符串类型的方法全部内容了,希望大家多多支持~ 以上是小编为您精心准备的的内容,在的博客.问答.公众号.人物.课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索jquery 字符串转int 字符串转换成int类型.字符串转换为int类

ASP实用技巧之类型转换函数

函数|技巧|转换 每个函数都可以强制将一个表达式转换成某种特定数据类型. 语法 CBool(expression) CByte(expression) CCur(expression) CDate(expression) CDbl(expression) CDec(expression) CInt(expression) CLng(expression) CSng(expression) CStr(expression) CVar(expression) CStr(expression) 必要的

asp Fix、Int、Round、CInt函数使用说明_ASP基础

Fix(number) 和 Int(number) 都是返回数字的整数部分. number 为正数时,二者返回值一样.比如:Fix(3.6)=3,Int(3.6)=3. number 为负数时,Fix 直接去除小数部分,Int 返回小于或等于 number 的第一个负整数.比如:Fix(-3.6)=-3,Int(-3.6)=-4. Round(number, numdecimalplaces),第二个参数表示从小数点右边第几位开始实行四舍五入,可以省略,默认是0,即四舍五入返回整数.CInt(n

asp.net C int[]数组转换成decimal[]数组实例

<!-- asp教程.net c int[]数组转换成decimal[]数组实例 --> <% int32[] arr = { 1, 2, 3 }; ilist<decimal> lists = new list<decimal>(); foreach (int32 i in arr) {  lists.add(convert.todecimal(i)); } decimal[] darr = lists.toarray<decimal>(); for

asp Fix、Int、Round、CInt函数使用说明

Fix(number) 和 Int(number) 都是返回数字的整数部分. number 为正数时,二者返回值一样.比如:Fix(3.6)=3,Int(3.6)=3. number 为负数时,Fix 直接去除小数部分,Int 返回小于或等于 number 的第一个负整数.比如:Fix(-3.6)=-3,Int(-3.6)=-4. Round(number, numdecimalplaces),第二个参数表示从小数点右边第几位开始实行四舍五入,可以省略,默认是0,即四舍五入返回整数.CInt(n

asp.net C# int 类型在32/64位环境下取值范围无变化

最近在学习中突然想到,我在64位环境下,int取值范围是否有变化?为了检测这个结果,我做了以下这个测试: 1.环境:win7旗舰版64位+vs2010 sp1(版本号:10.0.40219.1SP1Rel)+.Net 4.0.30319 SP1Rel 2.代码:  代码如下 复制代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleAppl

asp与js的类型转换函数介绍

字符串转化为整数 asp: cint()--注意只能转换短整数值的范围必须在-32768与32767之间,长整数应用clng() 如果输入的不是纯数字字符串或者空字符串,则直接500错误.所以输入前应该检查是否为数字类型.复制代码 代码如下:Function JCID(ByVal ParaValue)     If ((Not isNumeric(ParaValue)) or (Trim(ParaValue)="")) Then             JCID=0     Else

在javascript里 string 和 int 类型转换

string 转换为int 类型 (1)tostring()方法 var   x=10    a   =   x.toString()    //输出为string类型 alert(typeof(a)); (2)自动隐式转换 var   x=10;    a   =   x   +"";     //JS会自动隐性转换   //输出为string类型 alert(typeof(a));  int 转换为string类型 (1).Number方法 var s = "5"

C++中实现string类型转换int类型

C语言转换形式: ... std::string str; int i = atoi(str.c_str()); ... C++转换形式(C++11): ... std::string str; int i = std::stoi(str); ... 同样, 可以使用 stol(long), stof(float), stod(double) 等. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/ 以上是