无法从双精度转换为十进制错误

山姆

对于此分配,我需要对BankAccount程序进行一些处理,但首先需要复制运行示例的get。我已经完全按照下面的屏幕快照所示从工作分配表中复制了代码,但是出现以下错误。

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   

我不知道是什么原因导致了这些错误,因为我不认为我曾经使用过两次,而且很快对这些错误的Google都没有帮助。

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);

       }
    }
}
Heinzi

这里:

 BankAccount account = new BankAccount(142.50);

您正在传递double文字142.50但是,BankAccount希望使用decimal文字

 BankAccount account = new BankAccount(142.50m);

注意m数字后面。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将类型十进制转换为双精度

将IEEE-754双精度和单精度转换为十进制Java错误

隐式将十进制转换为双精度或将双精度转换为十进制

Spark for Python-无法将字符串列转换为十进制/双精度

将大十进制转换为不带指数格式的双精度

测试十进制值是否可以转换为双精度

C#如何精确地将双精度转换为十进制?

将字符串转换为双精度(非十进制)

将十进制数字从字符串转换为双精度

将计算出的双精度数转换为十进制会纠正精度误差吗?

如何在Matlab中将十进制转换为IEEE双精度浮点二进制?

如何在双精度双精度和十进制字符串之间转换?

将十进制转换为单精度IEEE 754

双精度十进制位数

将带有千位(和十进制)分隔符的字符串转换为双精度

将具有时间值“ 0:00”的字符串转换为十进制/双精度值?

在C ++中使用atof将带有十进制的字符串转换为双精度

如何将 Scala 数据框中的所有十进制列转换为双精度类型?

无法将十进制(4,2)转换为十进制(10,2)

如何将IEEE 754双精度(64位)格式的十六进制数字转换为十进制数字?

在SQL中将Varchar转换为十进制错误

无法将十进制数字转换为SqlDecimal

无法直接转换为十进制值?

无法将十进制转换为整数

TypeError(“无法将%r转换为十进制”%值)TypeError:无法将<LenderInvestment:20000.00>转换为十进制

EF检索错误的十进制精度

如何解决十进制和双精度之间的数学错误?

为什么 C# 会错误地计算双精度数和十进制数?

用Python将十进制转换为二进制半精度IEEE 754