Convert currency string to decimal?

后端 未结 6 930
北恋
北恋 2020-12-05 13:00

Objective

Sort a string that is displaying currency data like this $1,995.94 numerically in a set of data.

Code

I\'m cu

6条回答
  •  眼角桃花
    2020-12-05 13:28

    How about this, but only works for one string value. So you need to get your string split by $ and then do the conversion while saving into the array or list

     using System.Globalization;
        //rest of your code
    
              string str = "$50,550.20";
              decimal decval;
              bool convt = decimal.TryParse(str, NumberStyles.Currency,
                CultureInfo.CurrentCulture.NumberFormat, out decval);
              if (convt) 
              Console.WriteLine(decval);
              Console.ReadLine();
    

提交回复
热议问题