Online banking apps include a feature called Save the Change.
00:06
Whenever a transaction is made, the amount debited or taken from the account is rounded up to the nearest pound, with the difference being transferred to a savings account.
00:15
For example, if you purchase something for £8.90, then the 10 pence to round up to the nearest pound is transferred to savings.
00:24
We're going to write the Save to Change algorithm and and show you how to format numbers to two decimal places using what is known as a method.
00:33
As always, we start with the boilerplate code.
00:36
Next, we'll write a function that will take in the pounds and pence as a number and return the nearest pound from that amount.
00:43
So nine would be returned from 8.9.
00:47
Static int nearestpound.
00:51
This time we need some data.
00:53
So open bracket double because there'll be a decimal place in the number followed by the amount to be rounded up.
01:02
In our implementation of Save the Change, something will be saved in every transaction.
01:07
If the amount was exactly £9, then we'll round up to 10 and save a whole pound.
01:12
Therefore, a simple rounding command won't do.
01:16
Instead we'll convert the number to an integer and add one to it.
01:21
Casting command convert to int32 always truncates or removes any decimal places.
01:28
In effect, this rounds down.
01:32
Programming is as much about problem solving, thinking about what you want to achieve and then finding a solution to the problem.
01:39
Don't worry if you find it difficult to come up with a solution when you're learning programming.
01:43
This is actually the hardest part that comes with experience after you've seen and written lots of programmes.
01:51
Okay, now we'll write the function that, calculates the savings.
01:55
Static double save thechange and again we'll need the amount of the transaction, so this is in brackets.
02:06
Next we'll declare a variable for our savings double savingssemicolon.
02:11
We need to know if amount is a whole number to determine the amount to save.
02:17
If the whole number of the amount does not equal the actual amount, then savings becomes the nearest pound to the amount minus the amount.
02:27
It may be worth taking a moment here to understand the maths.
02:31
So if the amount is £8.90, then 8 does not equal £8.90.
02:37
So savings is the nearest pound, that's 9 minus £8.90, that's 0.1 or 10 pence otherwise.
02:49
Else savings is 1.
02:52
Because we always save a pound if the amount is a whole number, return the savings.
02:59
In the main programme, we can now use Console Write to ask the user to enter the purchase price.
03:06
We're going to store the input in a variable with decimal places.
03:09
So double purchase price equals convert todouble and the input in brackets.
03:17
Remember, inputs from the keyboard are always string data types, so they need converting to double numbers.
03:24
Now we'll calculate what the nearest pound is and store it in a variable called debit.
03:30
So int debit, an integer, because it will be a whole number, equals the value returned from our nearest pound function, which in brackets requires the purchase price.
03:41
Close the bracket colon.
03:44
You might notice that the nearest pound function we wrote earlier is expecting a parameter called amount.
03:50
But here we pass the argument purchaseprice.
03:53
That's okay.
03:54
What happens is purchase price is copied into the amount variable when the nearest pound function is called.
04:01
This is known as passing by value because we're parsing the value of one argument into one parameter.
04:10
Now we calculate the savings using our save the change function, again passing the value of the purchase price.
04:18
Finally, let's output the amount to be debited from the account and the amount to credit to savings console, writeline, open bracket, quote followed by our message.
04:29
Now, we want to output the variable debit to two decimal places.
04:33
We can do this using string format.
04:36
This method takes a string and uses formatting codes embedded within the string identified with curly brackets to format the output.
04:44
So open a bracket and a quote the word debit followed by a pound sign and then comes the formatting bit.
04:53
Open a curly bracket which signifies the formatting code, the first argument after the string.
04:59
So that's argument zero, a colon and then two decimal places, which has the code F2.
05:07
Close the curly bracket and close the string with a quote.
05:11
Now we specify the arguments for the string format method.
05:14
So comma, debit.
05:18
Whoa, that's quite a lot to take in.
05:20
Let's do it again.
05:21
For the savings console, writeline, open bracket, string format, open bracket followed by the string we want to format.
05:31
That's the words credit to savings and a pound sign.
05:35
Then a curly bracket to indicate where the formatting starts, zero, because there will be, the first variable here followed by a colon and then F2, which means format to two decimal places.
05:48
Close the curly bracket, close the string comma followed by the argument, which is savings.
05:55
Close the bracket and a colon.
05:57
Let's give the programme a run and check it works.