C# 打印小数点后的双位数
如何自定义打印双精度的函数?我希望此函数将小数点后的位数(在我的语言环境中为逗号)作为图形,然后打印,例如,带有用户指定的逗号后位数的pi。可能会帮助您使用该属性C# 打印小数点后的双位数,c#,.net,C#,.net,如何自定义打印双精度的函数?我希望此函数将小数点后的位数(在我的语言环境中为逗号)作为图形,然后打印,例如,带有用户指定的逗号后位数的pi。可能会帮助您使用该属性 var digits = 4; var myDouble = Math.PI; var formattedValue = myDouble.ToString("N" + digits.ToString(), CultureInfo.CurrentCulture); 来自MS的代码示例 Public Shared Sub M
var digits = 4;
var myDouble = Math.PI;
var formattedValue = myDouble.ToString("N" + digits.ToString(),
CultureInfo.CurrentCulture);
来自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
可能会帮助您使用该属性
来自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
我在某种程度上告诉你,我在我的项目中解决了一个类似的问题 将结果值转换为字符串。因为你用了π=3,14
String Result = "3,14";
使用
Split(Char())//使用逗号将其拆分为数组
现在您将获得一个数组,数组[1]将为您提供第二部分
因为它仍然是一个字符串,所以使用string.Length()
函数来获取长度(除了逗号后面的位数外,什么都没有)
现在,您可以按任何方式打印结果数字我告诉您,我在我的项目中解决了一个类似的问题 将结果值转换为字符串。因为你用了π=3,14
String Result = "3,14";
使用
Split(Char())//使用逗号将其拆分为数组
现在您将获得一个数组,数组[1]将为您提供第二部分
因为它仍然是一个字符串,所以使用string.Length()
函数来获取长度(除了逗号后面的位数外,什么都没有)
现在,您可以按任意方式打印结果数字您可以发布您拥有的示例吗?您可以发布您拥有的示例吗?CultureInfo.CurrentCulture->这不是necessary@gruber-我出于习惯而加入文化规范,因为如果你不这样做,代码分析/FxCop会对你咆哮。不管怎样,我很高兴这对你有用!CultureInfo.CurrentCulture->这不是necessary@gruber-我出于习惯而加入文化规范,因为如果你不这样做,代码分析/FxCop会对你咆哮。不管怎样,我很高兴这对你有用!