键入从十进制到字符串的转换 [英] Type Conversion from Decimal to String

查看:79
本文介绍了键入从十进制到字符串的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem,AchievedPer));

string stotal = Convert.ToDecimal(total).ToString();



lblTotal.Text = stotal; //字符串Label.text =(局部变量)字符串//转换失败..







请告诉我,为什么我无法在lblTotal.Text上得到结果..?

total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, AchievedPer));
string stotal = Convert.ToDecimal(total).ToString() ;

lblTotal.Text = stotal;//string Label.text=(local variable)string//Conversion failed..



Please let me know,why I'm unable to get the result at lblTotal.Text..?

推荐答案

尝试使用decimal.parseexact或tryparse方法,它可以帮助或从数据库查询中处理并通过使用ISnull('ColumnName','0.0')处理它,所以如果你的数据为null它将返回0.0并且你的转换为十进制不会影响。
try decimal.parseexact or tryparse method it may help or from take care from your database query and handle it by usign ISnull('ColumnName','0.0') so if your data it null it will return 0.0 and your conversion to decimal will not affect.


你应该使用 Decimal.TryParse方法 [ ^ ]



++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++
按要求添加示例:

You should use Decimal.TryParse Method[^]

++++++++++++++++++++++++++++++++
Example added as requested:
using System;

public class Program
{
    public static void Main()
    {
        string str = "123.45";

        Decimal num;

        if (Decimal.TryParse(str, out num))
            Console.WriteLine(num);
        else
            Console.WriteLine("Unable to parse '{0}'.", str);
    }
}


这篇关于键入从十进制到字符串的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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