为什么我需要解析如果对象值是十进制自己>。 [英] why I need to parse if object value is decimal iteself >.

查看:82
本文介绍了为什么我需要解析如果对象值是十进制自己>。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lblTotalHolidayEntitled.Text = Math.Round(decimal.Parse(objLeave.HolidayTotalThisYear.ToString()), 2).ToString();



这就是我正在尝试的...为什么我需要解析..如果对象value iteself是一个十进制..没有它不工作


this is what i am trying ... why i need to parsing .. if object value iteself is a decimal .. without that its not working

推荐答案

哇:)



你拿一个十进制值,将其转换为字符串,然后再次将其解析为十进制以将其舍入,并将其转换回字符串。



您可以尝试:



Wow :)

You take a decimal value, convert it to a string, then parse it again to a decimal to round it, and convert it back to string.

You could try :

lblTotalHolidayEntitled.Text = objLeave.HolidayTotalThisYear.ToString("# ##0.00");











or

lblTotalHolidayEntitled.Text = Math.Round(objLeave.HolidayTotalThisYear, 2).ToString();





这个我更简单,应该做你需要的。



在更多信息之后编辑:



如果HolidayTotalThisYear是一个可以为空的类型,那么你必须这样做:





This is much simpler and should do what you need.

EDIT after some more informations :

If HolidayTotalThisYear is a nullable type, then you have to do :

if (objLeave.HolidayTotalThisYear.HasValue) {
   lblTotalHolidayEntitled.Text = Math.Round(objLeave.HolidayTotalThisYear.Value, 2).ToString();
}


通过 decimal.TryParse [ ^ ]而不是decimal.Parse - 它检查它,如果它可以进行转换并报告true或false转换是否成功,而不抛出任何异常。
So run it through decimal.TryParse[^] instead of decimal.Parse - it checks it, does the conversion if it can and reports true or false whether the conversion succeeded or not, without throwing any exceptions.


静态TryParse方法用于确定字符串的表示是否是指定的有效数字类型。以下链接详细说明。



http://msdn.microsoft.com/en-us/library/bb384043.aspx [ ^ ]
The Static TryParse Method is to determine whether the representation of string is a specified valid numeric type or not. Below is the link gives in detail.

http://msdn.microsoft.com/en-us/library/bb384043.aspx[^]


这篇关于为什么我需要解析如果对象值是十进制自己>。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆