2010-12-07 4 views
1

두 개를 인쇄하는 기능을 어떻게 사용자 정의 할 수 있습니까? 이 함수는 소수점 (자국의 쉼표) 다음에 자릿수를 가져와 사용자가 지정한 쉼표 뒤에 숫자가있는 pi와 같이 인쇄 할 수 있습니다.소수점 뒤의 자릿수가 두 자리로 인쇄됩니다.

+0

보유하고있는 제품의 예를 게시 할 수 있습니까? – unholysampler

답변

5
var digits = 4; 
var myDouble = Math.PI; 

var formattedValue = myDouble.ToString("N" + digits.ToString(), 
    CultureInfo.CurrentCulture); 
+0

CultureInfo.CurrentCulture -> this is not needed – gruber

+0

@gruber - 코드 분석/FxCop이 없으면 습관 때문에 문화 사양을 포함합니다. 관계없이, 나는 이것이 당신을 위해 일해서 다행입니다! –

2

String.FormatNumberFormatInfo.NumberDecimalDigits 속성을 사용하여 당신을 도울 수 있습니다.

MS의 코드 예제.

Public Shared Sub Main() 

    //' Gets a NumberFormatInfo associated with the en-US culture. 
    Dim nfi As NumberFormatInfo = New CultureInfo("en-US", False).NumberFormat 

    //' Displays a negative value with the default number of decimal digits (2). 
    Dim myInt As Int64 = - 1234 
    Console.WriteLine(myInt.ToString("N", nfi)) 

    //' Displays the same value with four decimal digits. 
    nfi.NumberDecimalDigits = 4 
    Console.WriteLine(myInt.ToString("N", nfi)) 

End Sub 
-1

나는 내 프로젝트에서 비슷한 문제를 해결했다고 말하고 있습니다.

결과 값을 문자열로 변환하십시오. = 당신이 파이처럼 사용하기 때문에

String Result = "3,14"; 

3,14가

분할을 사용하여 (샤아는()) // 배열로 분할 쉼표를 사용

당신은 배열을 얻을 것이다 이제 array [1]이 두 번째 부분을 제공합니다.

여전히 문자열이므로 string.Length() 함수를 사용하여 Length (쉼표 뒤에 나오는 자릿수)를 가져옵니다.

원하는 결과로 결과 숫자를 인쇄 할 수 있습니다.

관련 문제