如何定製打印雙打的功能?我希望這個函數獲得小數點後面的位數(逗號在我的語言環境中)作爲基準,然後打印出用戶指定逗號後的數字位數的pi。在小數點後打印雙數字
1
A
回答
5
var digits = 4;
var myDouble = Math.PI;
var formattedValue = myDouble.ToString("N" + digits.ToString(),
CultureInfo.CurrentCulture);
+0
CultureInfo.CurrentCulture - >這不是必需的 – gruber 2010-12-07 22:22:54
2
String.Format可能會幫助你使用NumberFormatInfo.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
我以某種方式告訴你,我在我的項目中解決了類似的問題。
將結果值轉換爲字符串。既然你使用像PI = 3,14
String Result = "3,14";
使用
斯普利特(CHAR())//使用逗號來把它們分割陣列
你會得到一個數組現在,array [1]會給你第二部分。
由於它仍然是一個字符串,所以使用string.Length()
函數來獲取長度(除了逗號之後的數字數量)。
您現在可以以任何您想要的方式打印得到的數字
您可以發佈您擁有的示例嗎? – unholysampler 2010-12-07 21:36:17