# Answer should be a decimal, not rounded to whole number in while loop

Page 1 of 1

## 1 Replies - 1037 Views - Last Post: 02 November 2012 - 04:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298288&amp;s=5e58ed3229c0c6479821ff003048956c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ag7

Reputation: 0
• Posts: 2
• Joined: 01-November 12

# Answer should be a decimal, not rounded to whole number in while loop

Posted 01 November 2012 - 09:43 PM

I am trying to see how many years it will take for me to save a million dollars using the following conditions: monthlyWageAfterTaxes = 2700; numberOfYears=30;
Expenses.... rent=800; utilities=300; insurance=200; auto=250; food_misc=325; My rent increases \$200 every five years,first time in month 61 and my monthlyWageAfterTaxes increases 2% every year,first time in month 13.

Here is my code so far:
% input statements
monthlyWageAfterTaxes = 2700;
savingsGoal= input('Type in a savings goal: ');

%Expenses
rent=800;
utilities=300;
insurance=200;
auto=250;
food_misc=325;
totalSavings=0;
numYears=0;

while totalSavings < savingsGoal
monthlyBills= rent+utilities+insurance+auto+food_misc;

if rem(numYears,5) ==0;
rent = rent + 200;
end
totalSavings = totalSavings + monthlyWageAfterTaxes*12 - monthlyBills*12
monthlyWageAfterTaxes = monthlyWageAfterTaxes * 1.02;
numYears= (numYears+1)

end

My problem is that I want to show the number of years as a decimal instead of the code rounding to the nearest whole year. I worked it out on paper and I get 48.16667 years but when I run the code, the answer is 51 years. Can anyone see what my error is? I am trying to solve this problem by just using years, but do I need to solve using months and then divide by 12 at the end of the code?

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Answer should be a decimal, not rounded to whole number in while loop

### #2 sepp2k

• D.I.C Lover

Reputation: 2610
• Posts: 4,163
• Joined: 21-June 11

## Re: Answer should be a decimal, not rounded to whole number in while loop

Posted 02 November 2012 - 04:25 AM

When asking a question in the Other Languages forum, you should mention which language you're talking about - preferably in the title.