Decimal.Parse

本文讲解了如何在ASP.NET GridView的RowUpdating事件中处理用户输入的货币值,通过使用Decimal.Parse方法结合System.Globalization.NumberStyles.Currency确保正确解析各种格式的货币金额。
GridView的RowUpdating事件
GridView 的 RowUpdating 事件接受的第二个参数是一个 GridViewUpdateEventArgs 类型的对象,它包含一个 NewValues 字典,当中的每一个属性保存着用户输入的值,准备赋值到 ObjectDataSource 的 UpdateParameters 集合。我们可以重写现有的 NewValues 集合中的 UnitPrice 值为一个货币金额,通过下面的事件处理程序中的代码进行解析: 
protected   void  GridView1_RowUpdating( object  sender, GridViewUpdateEventArgs e)

{

 
if (e.NewValues["UnitPrice"!= null)

    e.NewValues[
"UnitPrice"= decimal.Parse(e.NewValues["UnitPrice"].ToString(), System.Globalization.NumberStyles.Currency);

}

Decimal.Parse

如果用户提供了一个诸如“$ 19.00 ” 的UnitPrice值,这个值会被通过Decimal.Parse计算并解析为货币金额的值重写。这将正确的解析货币值,无论是货币符号、逗号、小数点、等等,并使用System.Globalization命名空间的NumberStyles枚举

/// <summary> /// 版本更新 /// </summary> /// <param name="VersionCode">手机中已安装的版本号</param> /// <returns></returns> [CustomUnAuthorize] public ActionResult VersionUpdate(string VersionCode) { var result = string.Empty; string url = Server.MapPath("~"); url = "{0}{1}".FormatTo(url, "\\Content\\APPInstall"); var appFiles = Directory.GetFiles(url, "Suncere_YiChun_*.apk"); if (appFiles.Length == 0) { return Content(result); } Dictionary<decimal, string> apkVersionCodes = new Dictionary<decimal, string>(); foreach (var appFile in appFiles) { var fullName = appFile.Split('\\').LastOrDefault(); var newVersion = Path.GetFileNameWithoutExtension(fullName).Replace("Suncere_YiChun_", ""); var newV = decimal.Parse(newVersion.Replace(".", "")); apkVersionCodes[newV] = newVersion; } var maxCodeValue = apkVersionCodes.Max(x => x.Key); var oldV = decimal.Parse(VersionCode.Replace(".", "")); if (oldV >= maxCodeValue) { return Content(result); } string uri = HttpContext.Request.Url.Authority; result = "\"http://{0}/Content/APPInstall/{1}\"".FormatTo(uri, string.Format("Suncere_YiChun_{0}.apk", apkVersionCodes[maxCodeValue])); return Content(result); } 将 上面 MVC接口改为AppService 接口 [HttpGet] [Route("VersionUpdate")] public string VersionUpdate(string VersionCode) { var result = string.Empty; string url = Server.MapPath("~"); url = "{0}{1}".FormatTo(url, "\\Content\\APPInstall"); var appFiles = Directory.GetFiles(url, "Suncere_YiChun_*.apk"); if (appFiles.Length == 0) { return result; } Dictionary<decimal, string> apkVersionCodes = new Dictionary<decimal, string>(); foreach (var appFile in appFiles) { var fullName = appFile.Split('\\').LastOrDefault(); var newVersion = Path.GetFileNameWithoutExtension(fullName).Replace("Suncere_YiChun_", ""); var newV = decimal.Parse(newVersion.Replace(".", "")); apkVersionCodes[newV] = newVersion; } var maxCodeValue = apkVersionCodes.Max(x => x.Key); var oldV = decimal.Parse(VersionCode.Replace(".", "")); if (oldV >= maxCodeValue) { return result; } string uri = HttpContext.Request.Url.Authority; result = "\"http://{0}/Content/APPInstall/{1}\"".FormatTo(uri, string.Format("Suncere_YiChun_{0}.apk", apkVersionCodes[maxCodeValue])); return result; }
最新发布
08-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值