Round up to the nearest pound.
Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
x is assigned the value of y rounded down to the nearest integer.
F2
Is a formatting string meaning a fixed point number to be displayed to two decimal places.
E.g. The following statement:
Console.WriteLine($"{43.896:F2}");
will output 43.90
Questions to think about with this program to check your understanding:
Explain the purpose of using `int` in line 13 to cast the variable `amount` from a float to an integer.
To round down the number. When casting from one data type to another, data can be lost. We used this to our advantage in this example because we want to discard the decimal places resulting in the number being rounded down.
Why has the `format` method been used in line 24?
To make the output of debit two decimal places. The `format` method provides for the formatting of strings for output.
Change the program so that it:
Enter the purchase price: £3.40
Debit - £4.00
Credit to savings - £0.60
Enter the purchase price: £3
Debit - £3.00
Credit to savings - £0.00
Enter the purchase price: £10.23
Debit - £11.00
Credit to savings - £0.77
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
Enter the purchase price: £12
Debit - £12.00
Credit to savings - £0.00
Enter the purchase price: £4.50
Debit - £5.00
Credit to savings - £0.50
Enter the purchase price: £110.99
Debit - £111.00
Credit to savings - £0.01