How to convert a string to a decimal in C#

1 Answer

0 votes
using System;
   
class Program
{
    static void Main() {
        string str = "378483.901";
        decimal dec;

        if (Decimal.TryParse(str, out dec)) {
            Console.WriteLine("{0}, {1}", dec, dec.GetType());

        } else {
            Console.WriteLine("Conversion Error");
        }
    }
}
  
   
   
   
/*
run:
   
378483.901, System.Decimal
 
*/

 



answered Aug 4, 2022 by avibootz
edited Aug 4, 2022 by avibootz
...