If you run the following, fixed code:
void Main() { var sets=6; var price_bananas=0.88; var price_eggs=1.10; var price_berries=0.37; var amount_cash=40; var needed_product = sets * (2 * price_bananas) + sets * (4 * price_eggs) + sets * (0.2 * price_berries); var difference = Math.Abs(needed_product - amount_cash); if (amount_cash >= needed_product) { Console.Write( "Ivancho has enough money - it would cost " + "{0:F2} lv.", needed_product); } }
It produces the correct conclusion:
Ivancho has enough money - it will cost 37.40 BGN.
Explanation: Another way you tried to convert the number to a string inside (using the equivalent of the .ToString() method), and then apply the {0:F2} formatting, which became useless since the number is already a default formatted string options.
To avoid this, create a full format string, then add the parameters without changes (as shown in the code).
source share