Csharp/CSharp Tutorial/Data Type/decimal parse

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Decimal parse for currency

<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.Text; using System.Text.RegularExpressions; using System.Reflection; public class MainClass{

  public static void Main(){
       string[] money = new string[] { "$0.99", "$0,99", "$1000000.00", "$10.25", "$90,000.00", "$90.000,00", "$1,000,000.00", "$1,000000.00" };
       foreach (string m in money)
       {
           try
           {
               Decimal.Parse(m, NumberStyles.Currency);
               Console.WriteLine("!{0}: True", m);
           }
           catch (FormatException)
           {
               Console.WriteLine("!{0}: False", m);
           }
       }
  }

}</source>

!$0.99: True
!$0,99: True
!$1000000.00: True
!$10.25: True
!$90,000.00: True
!$90.000,00: False
!$1,000,000.00: True
!$1,000000.00: True

Parse decimal from string

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   try {  
     decimal d = Decimal.Parse("1234.1234");  
     Console.WriteLine(d);  
   } catch(FormatException exc) {  
     Console.WriteLine(exc.Message);  
     return;  
   }  
 }

}</source>

1234.1234