Csharp/CSharp Tutorial/Data Type/decimal parse
Версия от 15:31, 26 мая 2010; (обсуждение)
Decimal parse for currency
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);
}
}
}
}!$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
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;
}
}
}1234.1234