Set decimals to specific values

61 Views Asked by At

I'm working on a program that will calculate the change based on users input of price and amount paid. I shall then print the change to console followed by the amount of different bills and coins. This is all good until the final part, we are using 50-cents (halfCrown) in code. If the change is <0.25 i shall set the change to 0, if it's >0.25, <0.75 the change should be set to 0.5, if it's >0.75 it should be 1. My question is, how do I do this, I've browsed multiple forums for answers but haven't got anything to work.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Inlämningsuppgift_1__Växel
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Meddelande som välkomnar användaren.
            Console.WriteLine("Hej och välkommen till kassan, jag ska nu hjälpa dig att beräkna din växel!");

            // Deklarerar variablerna jag ska använda. Vissa har double då jag de ska kunna behandla decimaler. Int för de som är heltal
            double price;
            double paidAmount;
            double change;
            int thousandBills;
            int fiveHundredBills;
            int hundredBills;
            int fiftyBills;
            int twentyBills;
            int tenCrowns;
            int fiveCrowns;
            int oneCrown;
            double halfCrown;
           
            Console.Write("Mata in varans pris: "); // Ber användaren att mata in pris för varan.
            price = Convert.ToDouble(Console.ReadLine()); // Gör om användarens svar från string till double.
            Console.Write("Mata in ditt betalade belopp: "); // Ber användaren mata in betalat belopp.
            paidAmount = Convert.ToDouble(Console.ReadLine()); // Gör om användarens svar från string till double.
            change = paidAmount - price; // Tilldelar variablen "change" genom att subtrahera betalade beloppet från priset.
            Console.WriteLine("Din växel är: " + change + "kr"); // Skriver ut växeln användaren får med hjälp av variabeln "change".

            
            
            
                thousandBills = (int)change / 1000;
                change = change % 1000;
                Console.WriteLine("Antal 1000-sedlar: "+ thousandBills); // Skriver ut antal 1000-sedlar till konsolen.

                fiveHundredBills = (int)change / 500;
                change = change % 500;
                Console.WriteLine("Antal 500-sedlar: " + fiveHundredBills); // Skriver ut antal 500-sedlar till konsolen.

                hundredBills = (int)change / 100;
                change = change % 100;
                Console.WriteLine("Antal 100-sedlar: " + hundredBills); // Skriver ut antal 100-sedlar till konsolen.

                fiftyBills = (int)change / 50;
                change = change % 50;
                Console.WriteLine("Antal 50-sedlar: "+ fiftyBills); // Skriver ut antal 50-sedlar till konsolen.

                twentyBills = (int)change / 20;
                change = change % 20;
                Console.WriteLine("Antal 20-sedlar: " +  twentyBills); // Skriver ut antal 20-sedlar till konsolen.

                tenCrowns = (int)change / 10;
                change = change % 10;
                Console.WriteLine("Antal 10-mynt: " +  tenCrowns); // Skriver ut antal 10-kronor till konsolen.

                fiveCrowns = (int)change / 5;
                change = change % 5;
                Console.WriteLine("Antal 5-mynt: " + fiveCrowns); // Skriver ut antal 5-kronor till konsolen.

                oneCrown = (int)change / 1;
                change = change % 1;
                Console.WriteLine("Antal 1-mynt: " +  oneCrown); // Skriver ut antal 1-kronor till konsolen.

                
                if (change < 0.25)
                {
                    change = 0;
                    
                }
                else if (change > 0.25 && change < 0.75)
                {
                    change = 0.5;
                }
                else 
                {
                            change++;
                }

                }
                
                change = Math.Round(change * 4, MidpointRounding.ToEven) / 4;
                halfCrown = (double)change * 2;
                change = change % 0.5;
                Console.WriteLine("Antal 50-öre: " + halfCrown); // Skriver ut antal 50-ören till konsolen.

            
            // Försökt att få in dessa på lämpliga ställen men inte kommit fram till ett lyckat resultat.
            // change = Math.Round(change * 4, MidpointRounding.ToEven) / 4;
            // change = Math.Round(change * 2.0d / 2.0d);
            Console.ReadLine();
            

        }

    }
}

I've tried the last if loop and other code down below that but nothing works.

1

There are 1 best solutions below

4
Grant On BEST ANSWER

What about this:

change = Math.Round(change * 2) / 2;