PowerShell
function factorial( [int] $n )
{
$result = 1;
if ( $n -gt 1 )
{
$result = $n * ( factorial ( $n - 1 ) )
}
$result
}
Вот острота:
$n..1 | % {$result = 1}{$result *= $_}{$result}
Используйте функцию Decimal.TryParse .
decimal value;
if(Decimal.TryParse(strOrderId, out value))
// It's a decimal
else
// No it's not.
decimal decValue;
if (decimal.TryParse(strOrderID, out decValue)
{ / *this is a decimal */ }
else
{ /* not a decimal */}
Вы можете использовать Decimal.TryParse , чтобы проверить, можно ли преобразовать значение в десятичный тип. Вы также можете использовать Double.TryParse вместо этого, если присвоите результат переменной типа Double.
Пример MSDN:
string value = "1,643.57";
decimal number;
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
вы можете попробовать разобрать его:
string value = "123";
decimal result;
if (decimal.TryParse(value, out result))
{
// the value was decimal
Console.WriteLine(result);
}