Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert from double to decimal error

Tags:

c#

For this assignment I need to do some stuff with a BankAccount program, but first I need to copy the get the example running. I've copied the code from the assignment sheet exactly as shown in the screenshots below, but I'm getting the errors shown below.

Error   2   Argument 1: cannot convert from 'double' to 'decimal' Line 13 Column 51
Error   1   The best overloaded method match for 'BankAccount.BankAccount.BankAccount(decimal)' has some invalid arguments Line 13 Column 35    
Error   4   Argument 1: cannot convert from 'double' to 'decimal' Line 13 Column 30 
Error   3   The best overloaded method match for 'BankAccount.BankAccount.Withdraw(decimal)' has some invalid arguments Line 18 Column 13   

I have no idea what's causing these errors as I don't think I've used a double once, and a very quick Google of the errors didn't help me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccount
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create Bank Account & Print Balance
            BankAccount account = new BankAccount(142.50);
            Console.WriteLine("Account Balance is: " + account.ToString());

            // Withdraw £30.25
            Console.WriteLine("Withdrawing £30.25");
            account.Withdraw(30.25);

            // Print balance again
            Console.WriteLine("Account Balance is: " + account.ToString());
        }
    }

.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccount
{
   public class BankAccount
    {
       private decimal _balance;

       public decimal Balance
       {
           get { return _balance; }
           private set { _balance = value; }
       }

       //Constructor: Constructs a new Bank Account with 0 balance
       public BankAccount()
       {
           Balance = 0;
       }

       //Constructor: Constructs a new Bank Account with the specified balance
       public BankAccount(decimal balance)
       {
           Balance = balance;
       }

       //Deposits the specified amount into the Bank Account
       public void Deposit(decimal amount)
       {
           Balance += amount;
       }

       //Withdraws the specified amount from the Bank Account
       public void Withdraw(decimal amount)
       {
           Balance -= amount;
       }

       //ToString Override
       public override string ToString()
       {
           return string.Format("{0}: Balance = {1}", "BankAccount", Balance);

       }
    }
}
like image 220
Sam Avatar asked Nov 04 '14 14:11

Sam


2 Answers

Here:

 BankAccount account = new BankAccount(142.50);

you are passing the double literal 142.50. BankAccount, however, expects a decimal literal instead:

 BankAccount account = new BankAccount(142.50m);

Note the m behind the number.

like image 110
Heinzi Avatar answered Oct 13 '22 00:10

Heinzi


There is simple answer (and even example) in MSDN for C#'s decimal.

You've right to use decimal for this kind of operation:

The decimal keyword indicates a 128-bit data type. Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

And using decimal literals requires m suffix:

If you want a numeric real literal to be treated as decimal, use the suffix m or M, for example:

decimal myMoney = 300.5m;

Without the suffix m, the number is treated as a double and generates a compiler error.

Also two more import points from docs:

  • There is no implicit conversion between floating-point types and the decimal type; therefore, a cast must be used to convert between these two types.

  • You can also mix decimal and numeric integral types in the same expression. However, mixing decimal and floating-point types without a cast causes a compilation error.

This is important so you won't slowly build up error for example by summing up many 0.1 which is periodic number in binary.

like image 37
Vyktor Avatar answered Oct 13 '22 00:10

Vyktor