2014-11-04 8 views
0

이 과제에서 나는 BankAccount 프로그램으로 몇 가지 작업을 수행해야하지만, 먼저 예제를 실행시켜야합니다. 아래의 스크린 샷과 같이 과제 시트의 코드를 복사했지만 아래에 오류가 표시됩니다.이중에서 소수점 이하의 오류로 변환 할 수 없음

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 

나는 내가 한 번 두 번 사용했습니다 생각하지 않으며, 오류의 매우 빠른 구글이 나에게 도움이되지 않았다 이러한 오류의 원인 무엇인지 전혀 모른다.

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

     } 
    } 
} 
+2

사진 대신에 질문의 본문에 코드를 게시하십시오. 또한 코드에서 오류가 발생한 위치를 알려줌으로써 상황이 어디에서 발생하는지 파악할 필요가 없습니다. –

+6

소수점 리터럴은'm' 접미사로 표시되므로 생성자에'decimal' 인수가 필요한 경우'new BankAccount (142.50m) '을 사용해야합니다. – Lee

+0

나는 캐스트가없는 십진수로 전달하려고하는 double을 반환하는 함수를 가지고있을 것입니다. 즉, 내 작업 블록 이후로 코드를 볼 수 없습니다. – IllusiveBrian

답변

6

:

BankAccount account = new BankAccount(142.50); 

당신이 double 문자 142.50을 전달하고 있습니다. 는 BankAccount는, 그러나, 대신 decimal literal 기대 :

BankAccount account = new BankAccount(142.50m); 

참고 숫자 뒤에 m.

+0

신난다. 그것은 그것이 분류된다. StackOverflow를 사용하면 10 분 안에 응답을받을 수 있습니다. 과제 시트에이 오류가 포함 된 이유가 확실하지 않은 경우 실험실 세션에서 누군가가 깨달았을 때 오류가 있음을 지적했을 것입니다.하지만 불행히도 그것을 통해 잠을 잤습니다 ... – Sam

+0

메모를 추가해야합니다. 'Withdraw' 메서드 나 다른 곳에서는 십진수를 사용하는 경우 m을 사용합니다. –

2

MSDN for C#'s decimal에는 간단한 대답 (심지어 예)이 있습니다.

당신은 작동 이런 종류의 decimal를 사용하는 권리했습니다 :

소수 키워드는 128 비트 데이터 형식을 나타냅니다. 부동 소수점 유형과 비교할 때 십진 유형은 정밀도와 범위가 더 작으므로 재정적 및 금전적 계산에 적합합니다.

그리고 decimal 리터럴을 사용하여 m 접미사가 필요합니다

당신은 예를 들어 숫자 소수로 취급 리터럴 실제 사용하는 접미사 m 또는 M, 원하는 경우 :

decimal myMoney = 300.5m;

접미어 m이 없으면 숫자는 double로 처리되어 컴파일러 오류가 발생합니다. 문서에서

또한 두 개 더 가져 오기 포인트 :

  • 부동 소수점 형식과 소수 형 사이에 암시 적 변환이 없습니다; 따라서이 두 유형간에 변환하려면 형 변환을 사용해야합니다.

  • 동일한 표현식에서 10 진수 및 숫자 정수 계열을 혼합 할 수도 있습니다. 그러나 캐스트없이 10 진수 및 부동 소수점 유형을 혼합하면 컴파일 오류가 발생합니다.

천천히 0.1 which is periodic number in binary 많은을 합산하여 예를 들어 오류를 구축하지 않도록이 중요하다.

관련 문제