gpt4 book ai didi

c# - 验证我的钱

转载 作者:太空狗 更新时间:2023-10-29 22:20:00 24 4
gpt4 key购买 nike

我在验证资金输入时遇到了一些问题。我使用了其他问题中的一些指示来编写一些更好的代码。以下是我用来验证输入是否为金钱的内容。

static void Main(string[] args)
{
string myTest1 = "$1,234.56";
string myTest2 = "$1.00";
string myTest3 = "$1000.01";
string myTest4 = "$1,234,567.89";

myIsMaybeMoneyValidator Miimv = new myIsMaybeMoneyValidator();

bool myResult1 = Miimv.isMaybeMoney(myTest1);
bool myResult2 = Miimv.isMaybeMoney(myTest2);
bool myResult3 = Miimv.isMaybeMoney(myTest3);
bool myResult4 = Miimv.isMaybeMoney(myTest4);
}

public bool isMaybeMoney(object theirMaybeMoney)
{
string myMaybeMoney = theirMaybeMoney.ToString();

if (myMaybeMoney.StartsWith("-"))
{
myMaybeMoney.Remove(0, 1);
}

if (!myMaybeMoney.StartsWith("$"))
{
return false;
}

myMaybeMoney.Remove(0, 1);

string[] myMaybeMoneyStringArray = myMaybeMoney.Split('.');

string myMaybeMoneyDollars = myMaybeMoneyStringArray[0];
string myMaybeMoneyCents = myMaybeMoneyStringArray[1];

if (!isDollars(myMaybeMoneyDollars))
{
return false;
}

if (!isCents(myMaybeMoneyCents))
{
return false;
}

return true;
}

private bool isDollars(string theirMaybeMoneyDollars)
{
if (!isNumber(theirMaybeMoneyDollars))
return false;

try
{
int myMaybeDollars = Convert.ToInt32(theirMaybeMoneyDollars);

if (myMaybeDollars < 1)
return false;

return true;
}
catch (Exception)
{
return false;
}

return true;
}

private bool isCents(string theirMaybeMoneyCents)
{
if (!isNumber(theirMaybeMoneyCents))
return false;

try
{
int myMaybeCents = Convert.ToInt32(theirMaybeMoneyCents);

if (myMaybeCents > 99)
return false;
if (myMaybeCents < 1)
return false;

return true;
}
catch (Exception)
{
return false;
}

return true;
}

private bool isNumber(object theirMaybeNumber)
{
return Microsoft.VisualBasic.Information.IsNumeric(theirMaybeNumber);
}

结果都不是钱,这让我很困惑。

最佳答案

看起来在 C# 中有一个内置的货币验证。

float num;
bool isValid = float.TryParse(str,
NumberStyles.Currency,
CultureInfo.GetCultureInfo("en-US"), // cached
out num);

来自: https://stackoverflow.com/a/617847/290822

关于c# - 验证我的钱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9182858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com