2013-09-25 3 views
1

totalPrice를 계산하고 totalPriceOutputLabel에 반환하는 함수가 있습니다. 내 문제는 출력 형식을 "1,222"로 형식화해야합니다. 나는 그걸로 변환하는 방법을 알고있다.Visual Basic에서 통화 형식으로 문자열 변환

ToString("C2") 

그러나 나는 나의 함수 호출 내에서 그것을 붙이는 방법에 관해 확신 할 수 없다. 어떤 아이디어? 다만이 같은

Public Class tileLimitedForm 

Private enteredLength, enteredWidth As Double 
Private enteredPrice As Decimal 

Public Function area(ByRef enteredLength As Double, ByRef enteredWidth As Double) 
    area = Val(enteredLength) * Val(enteredWidth) 
End Function 

Public Function totalPrice(ByRef enteredLength As Double, ByRef enteredWidth As Double) 
    totalPrice = Val(area(enteredLength, enteredWidth)) * Val(enteredPrice) 
End Function 

Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click 

totalPriceOutputLabel.Text = totalPrice(area(enteredLength, enteredWidth),enteredPrice).ToString("C2") 

End Sub 
+1

* * calculateButton_Click * 및 * totalPrice *가 (서식 제외) 동일한 작업을 수행하는 이유는 무엇입니까? 편집하는 동안 오류가 발생 했습니까? (* totalPrice * 함수는 현재 totalPrice *를 호출합니다.) – Chris

+0

내 실수였습니다. 원래 게시를 업데이트했습니다. 감사합니다 – user1695704

+0

잘 했어. 고맙습니다. – user1695704

답변

1

: 그것은 totalPrice을 가정한다

totalPriceOutputLabel.Text = _ 
    totalPrice(area(enteredLength, enteredWidth), enteredPrice).ToString("C2") 

는 형식 매개 변수를 사용하여 .ToString() 확장을 지원하는 Double 또는 다른 숫자 유형입니다.

Public Class tileLimitedForm 

     Private enteredLength, enteredWidth As Double 
     Private enteredPrice As Decimal 

     Public Function area(ByVal enteredLength As Double, ByVal enteredWidth As Double) As Double 
      area = enteredLength * enteredWidth 
     End Function 

     Public Function totalPrice(ByVal enteredLength As Double, ByvalenteredWidth As Double) As Double 
      totalPrice = area(enteredLength, enteredWidth) * enteredPrice 
     End Function 

     Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click 
      totalPriceOutputLabel.Text = totalPrice(area(enteredLength, enteredWidth), enteredPrice).ToString("C2") 
     End Sub 
    End Class 

참고 :

  • 당신은이 경우
  • 함수에 함수 대신 ByRefByVal를 사용해야합니다

    편집

    편집 된 질문을보고 한 후 현재을 반환합니다.함수를 반환하는 유형을 설정하지 않았으므로 (Option Strict가 해제되어 있음) =>As Double을 추가했습니다.

  • 매개 변수가 이미 숫자 유형이므로 Val을 사용할 필요가 없습니다.
+0

작성한 것을 구현할 때 ("C2")를 가리키는 처리되지 않은 예외와 관련된 오류가 발생합니다. 이 오류는 이것을 처리하는 방법을 실제로 설명하지 않습니다. – user1695704

+0

@ user1695704 어떤 함수가 * totalPrice * 함수를 반환합니까? 당신은 전체 방법으로 질문을 편집합니까? – Chris

+0

메소드가 업데이트되었습니다. 고맙습니다. – user1695704