货币型的数据比如:¥23,332.00,这个我在后台需要计算,我采取的方法是把它转换为字符型 ,然后再replace,再转换为decimal,本人学艺不精,请大家指教有什么好方法?

解决方案 »

  1.   

    本人也是先replace,再转。看来也属于学艺不精的。
      

  2.   

    直接转换?decimal.parse("¥23,332.00")?
      

  3.   

    lblInfo.Text = decimal.Parse("¥23,332.00").ToString();
    输入字符串的格式不正确。
      

  4.   

    直接转换decimal d = Decimal.Parse("¥23,332.00",NumberStyles.Currency);
      

  5.   

    如果不是我学艺实在太不精了,那从数据库里取出的money类型的数据应该是没有前面的货币符号吧,直接decimal
    如果是从页面取的,那也可以通过设计避免一下
    毕竟货币涉及数目和币种,不一定非放在一个单元里显示吧
      

  6.   

    Decimal.Parse("¥23,332.00",NumberStyles.Currency);
    输入字符串的格式不正确。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误: 
    行 10:     protected void Page_Load(object sender, EventArgs e)
    行 11:     {
    行 12:         Decimal.Parse("¥23,332.00", NumberStyles.Currency);
    行 13:     }
    行 14: }
     
      

  7.   

    如果CurrentCulture不是zh-CN,那么需要 CultureInfo ci = CultureInfo.CreateSpecificCulture("zh-CN");然后得到NumberStyle
      

  8.   

    17楼也不对
    Response.Write(CultureInfo.CurrentCulture.Name);查看了下就是zh-CN
      

  9.   

    string price ="¥123,123,123.012";
    decimal  dec = Decimal.Parseprice(price.Substring(1)); 
      

  10.   

    17楼   Decimal.Parse("¥23,332.00", NumberStyles.Currency); 
    不对   是因为    ¥   和  NumberStyles.Currency   不是一种编码的钱的形式 。
      

  11.   

    ¥这个不是一个符号吗?
    Decimal类型难道自带该符号?晕
    你把¥放一个label里面 23,000.00放另一Label里面不就可以转换了吗
      

  12.   

    ¥23,332.00是由数据转换而来,或者说是data.ToString("C")来的
      

  13.   

    你可以直接tostring()自己添加 ¥嘛
      

  14.   

    data.ToString()
    里面有提供的格式化的说明,不需要自己添加','
      

  15.   


    是啊,这个数据就是我用ToString("C")而来的,就有一个¥
      

  16.   

     Decimal.Parse("¥23,332.00".Substring(1)).ToString()
      

  17.   

                decimal d1 = 4324324242;
                decimal d = decimal.Parse(d1.ToString("C"), System.Globalization.NumberStyles.Currency | System.Globalization.NumberStyles.AllowCurrencySymbol);
      

  18.   


                decimal d = 1234.45M;
                Console.WriteLine(d);
                string money = d.ToString("C");
                decimal result;
                decimal.TryParse(money, System.Globalization.NumberStyles.Currency, null, out result);
                Console.WriteLine(result);
      

  19.   

    这是MSDN上的例子,自己看吧!// Example for the Decimal.ToString( ) methods.
    using System;
    using System.Globalization;
    using Microsoft.VisualBasic;class DecimalToStringDemo
    {
        static void Main( )
        {
            decimal nineBillPlus = 9876543210.9876543210M;
                
            Console.WriteLine( "This example of\n" +
                "   Decimal.ToString( ), \n" +
                "   Decimal.ToString( String ),\n" +
                "   Decimal.ToString( IFormatProvider ), and \n" +
                "   Decimal.ToString( String, IFormatProvider )\n" +
                "generates the following output when run in the " +
                "[{0}] culture.\nDecimal numbers are formatted " +
                "with various combinations \nof format strings " +
                "and IFormatProvider.", 
                CultureInfo.CurrentCulture.Name );        // Format the number without and with format strings.
            Console.WriteLine( "\nIFormatProvider is not " +
                "used; the default culture is [{0}]:", 
                CultureInfo.CurrentCulture.Name );
            Console.WriteLine( "   {0,-30}{1}", "No format string:", 
                nineBillPlus.ToString( ) );
            Console.WriteLine( "   {0,-30}{1}", "'N' format string:", 
                nineBillPlus.ToString( "N" ) );
            Console.WriteLine( "   {0,-30}{1}", "'N5' format string:", 
                nineBillPlus.ToString( "N5" ) );
                
            // Create a CultureInfo object for another culture. Use
            // [Dutch - The Netherlands] unless the current culture
            // is Dutch language. In that case use [English - U.S.].
            string cultureName = 
                CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) == 
                "nl" ? "en-US" : "nl-NL";
            CultureInfo culture = new CultureInfo( cultureName );
                
            // Use the CultureInfo object for an IFormatProvider.
            Console.WriteLine( "\nA CultureInfo object " +
                "for [{0}] is used for the IFormatProvider: ", 
                cultureName );
            Console.WriteLine( "   {0,-30}{1}", "No format string:", 
                nineBillPlus.ToString( culture ) );
            Console.WriteLine( "   {0,-30}{1}", "'N5' format string:", 
                nineBillPlus.ToString( "N5", culture ) );
                
            // Get the NumberFormatInfo object from CultureInfo, and
            // then change the digit group size to 4 and the digit
            // separator to '_'.
            NumberFormatInfo numInfo = culture.NumberFormat;
            numInfo.NumberGroupSizes = new int[ ] { 4 };
            numInfo.NumberGroupSeparator = "_";
                
            // Use a NumberFormatInfo object for IFormatProvider.
            Console.WriteLine( 
                "\nA NumberFormatInfo object with digit group " +
                "size = 4 and \ndigit separator " +
                "= '_' is used for the IFormatProvider:" );
            Console.WriteLine( "   {0,-30}{1}", "'N5' format string:", 
                nineBillPlus.ToString( "N5", culture ) );
        } 
    } /*
    This example of
       Decimal.ToString( ),
       Decimal.ToString( String ),
       Decimal.ToString( IFormatProvider ), and
       Decimal.ToString( String, IFormatProvider )
    generates the following output when run in the [en-US] culture.
    Decimal numbers are formatted with various combinations
    of format strings and IFormatProvider.IFormatProvider is not used; the default culture is [en-US]:
       No format string:             9876543210.9876543210
       'N' format string:            9,876,543,210.99
       'N5' format string:           9,876,543,210.98765A CultureInfo object for [nl-NL] is used for the IFormatProvider:
       No format string:             9876543210,9876543210
       'N5' format string:           9.876.543.210,98765A NumberFormatInfo object with digit group size = 4 and
    digit separator = '_' is used for the IFormatProvider:
       'N5' format string:           98_7654_3210,98765
    */