2010-12-07 99 views
1

如何定制打印双打的功能?我希望这个函数获得小数点后面的位数(逗号在我的语言环境中)作为基准,然后打印出用户指定逗号后的数字位数的pi。在小数点后打印双数字

+0

您可以发布您拥有的示例吗? – unholysampler 2010-12-07 21:36:17

回答

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()函数来获取长度(除了逗号之后的数字数量)。

您现在可以以任何您想要的方式打印得到的数字