C#如何在不进行舍入的情况下将双精度数组格式化为一位小数

我需要将double值格式化为一个小数位而不进行舍入。

double value = 3.984568438706 string result = ""; 

我试过的是:

1)

 result = value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture) + "%"; // returns 3.98% 

2)

 result = value.ToString("##.#", System.Globalization.CultureInfo.InvariantCulture) + "%"; // returns 4% 

3)

  result = value.ToString("##.0", System.Globalization.CultureInfo.InvariantCulture) + "%"; // returns 4.0% 

4)(以下其他建议)

 value = (value / 100); result = String.Format("{0:P1}", Math.Truncate(value * 10000) / 10000); // returns 4.0% result = string.Format("{0:0.0%}",value); // returns 4.0% 

我需要显示的是3.9%的值

在此先感谢您的帮助。

 result=string.Format("{0:0.0}",Math.Truncate(value*10)/10); 

我会做一个实用工具方法来处理这个:

 static double Truncate(double value, int digits) { double mult = System.Math.Pow(10.0, digits); return System.Math.Truncate(value * mult) / mult; } 

然后你可以这样做:

 result = Truncate(value, 1).ToString("##.#", System.Globalization.CultureInfo.InvariantCulture) + "%"; 

请注意,您可能还需要Math.Floor而不是truncate – 但这取决于您希望如何处理负值。

我知道这是一个老线程,但我必须这样做。 虽然这里的方法有效,但我想要一种简单的方法来影响很多调用,所以在对string.format的所有调用中使用Math.Truncate并不是一个好的选择。

因此,我创建了一个自定义格式提供程序,它允许我在格式化字符串中添加截断,例如

 string.format(new FormatProvider(), "{0:T}", 1.1299); // 1.12 string.format(new FormatProvider(), "{0:T(3)", 1.12399); // 1.123 string.format(new FormatProvider(), "{0:T(1)0,000.0", 1000.9999); // 1,000.9 

实现非常简单,可以轻松扩展到其他要求。

 public class FormatProvider : IFormatProvider, ICustomFormatter { public object GetFormat(Type formatType) { if (formatType == typeof (ICustomFormatter)) { return this; } return null; } public string Format(string format, object arg, IFormatProvider formatProvider) { if (arg.GetType() != typeof (double)) { try { return HandleOtherFormats(format, arg); } catch (FormatException e) { throw new FormatException(string.Format("The format of '{0}' is invalid.", format)); } } if (format.StartsWith("T")) { int dp = 2; int idx = 1; if (format.Length > 1) { if (format[1] == '(') { int closeIdx = format.IndexOf(')'); if (closeIdx > 0) { if (int.TryParse(format.Substring(2, closeIdx - 2), out dp)) { idx = closeIdx + 1; } } else { throw new FormatException(string.Format("The format of '{0}' is invalid.", format)); } } } double mult = Math.Pow(10, dp); arg = Math.Truncate((double)arg * mult) / mult; format = format.Substring(idx); } try { return HandleOtherFormats(format, arg); } catch (FormatException e) { throw new FormatException(string.Format("The format of '{0}' is invalid.", format)); } } private string HandleOtherFormats(string format, object arg) { if (arg is IFormattable) { return ((IFormattable) arg).ToString(format, CultureInfo.CurrentCulture); } return arg != null ? arg.ToString() : String.Empty; } } 

ToString()不会这样做。 您必须添加额外的代码。 其他答案显示数学方法,我的方法下面是一种开箱即用的方法。

 string result = value.ToString(); Console.WriteLine("{0}", result.Substring(0, result.LastIndexOf('.') + 2)); 

这是一种相当简单的蛮力方法,但当小数为’。’时它就可以实现。 这是一种缓解疼痛的扩展方法(并处理小数点)。

 public static class Extensions { public static string ToStringNoTruncate(this double me, int decimalplaces = 1) { string result = me.ToString(); char dec = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]; return result.Substring(0, result.LastIndexOf(dec) + decimalplaces + 1); } } 
 ( Math.Truncate( ( value * 10 ) ) / 1000 ).ToString( "#.#%" ) 

只需使用modulo operator +内置ToString

 result = (value - (value % 0.1)).ToString("N1") + "%";