感謝勞倫斯伯克在isMaybeMoney函數的其他問題,我能夠確定輸入是否是金錢。計算利息無限?
我現在在做的是試圖計算興趣後的總數,但我一直讓Infinity寫入屏幕。我感興趣的功能是什麼在世界上是錯誤的?如果我用1,234美元作爲3.5%利息的起始餘額,應該是3,522.55美元。
有人可以幫我嗎?
static float money;
static void Main()
{
string[] myMaybeBalances = Accounts.GetStartingBalances();
myIsMaybeMoneyValidator Miimv = new myIsMaybeMoneyValidator();
ArrayList interests = Miimv.interestsAccrued(myMaybeBalances);
foreach (object interest in interests)
{
Console.WriteLine(interest);
}
Console.ReadLine();
}
public ArrayList interestsAccrued(string[] myMaybeBalances)
{
ArrayList interests = new ArrayList();
foreach (string myMaybeBalance in myMaybeBalances)
{
bool myResult = isMaybeMoney(myMaybeBalance);
if (myResult == true)
{
decimal[] rates = Accounts.GetRates();
for (int i = 0; i < rates.Length; i++)
{
decimal rate = rates[i];
float total = 1;
int n_X_t = 360;
while (n_X_t != 0)
{
rate = (1 + rates[i]/12);
float myRate;
float.TryParse(rate.ToString(), out myRate);
total = total * myRate;
total = total * money;
n_X_t = n_X_t - 1;
}
interests.Add(total);
}
}
}
return interests;
}
public bool isMaybeMoney(object theirMaybeMoney)
{
string myMaybeMoney = theirMaybeMoney.ToString();
float num;
bool isValid = float.TryParse(myMaybeMoney,
NumberStyles.Currency,
CultureInfo.GetCultureInfo("en-US"), // cached
out num);
money = num;
return isValid;
}
你爲什麼在所有的時間之間浮動和小數之間轉換?浮點數字計算不適合 - 擺脫它。 (爲什麼你不使用.NET命名約定或泛型?) – 2012-03-07 23:59:36
這是幹什麼的:'total = total * money;'? – 2012-03-08 00:00:52
'rates [0]'的值是多少? – 2012-03-08 01:28:22