[英]format decimal values in grid
我正在嘗試為DB中為十進制的一些檢索字段實現自定義格式。 我的價格字段中有1122.454540000
數據。 我想在自定義格式后在網格中顯示它。 如果小數點后的前四個字段中的至少一個不為零,我想顯示小數點后的四位數。 如果我有像1122.000100
這樣的字段,我想顯示為1122.0001
但是我有像9876.000000
這樣的值,那么我只想顯示9876,沒有任何小數點。 目前,一切正常
string.Format("{0:F4}", Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Price")))
但是我有一個特定案例的問題。 當我得到類似1122.4000
或1122.5400
值時,它僅顯示1122.4
和1122.54
而不顯示所有四個數字,而不管最后的尾隨數字是否為零。 我知道朝正確的方向邁進嗎?
可以修改下面的解決方案。 但基本上,您只想從字符串中刪除.00..
。
您可以使用RegEx來做到這一點:
public string Format(string format, double value)
{
var regex = new Regex(@"^(?<IntPart>.*)([.,]0*)$");
var s = String.Format("{0:F4}", value);
var match = regex.Match(s);
if (match.Success)
{
return match.Groups["IntPart"].Value;
}
return s;
}
Console.WriteLine(Format("{0:F4}", 1.35687)); // 1.3569
Console.WriteLine(Format("{0:F4}", 1.35)); // 1.3500
Console.WriteLine(Format("{0:F4}", 1)); // 1
Console.WriteLine(Format("{0:F4}", 42.000001)); // 42
您需要測試值的小數部分(准確為0)。
我認為要很好地執行它的唯一方法是使用ICustomFormatter 。
我將創建 F_XX格式,即F0或FXX,具體取決於變量是否為整數。 (在您的情況下, F_4將為F0或F4 )。
這是第一次嘗試:
public class MyCustomFormatter : IFormatProvider, ICustomFormatter
{
// Match "F_XX" where XX are digits.
private readonly Regex _regex = new Regex("F_(?<DigitCount>\\d+)");
// IFormatProvider.GetFormat implementation.
public object GetFormat(Type formatType)
{
// Determine whether custom formatting object is requested.
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
var shouldUseF0 = false;
var match = _regex.Match(format);
// Detect F_XX format.
if (match.Success)
{
// Manage float.
if (arg is float)
{
if (((float) arg)%1 == 0)
{
shouldUseF0 = true;
}
}
// Manage double.
if (arg is double)
{
if (((double) arg)%1 == 0)
{
shouldUseF0 = true;
}
}
// TODO: Manage int, long...
if (shouldUseF0)
{
format = "F0";
}
else
{
// Build the FXX format.
format = "F" + match.Groups["DigitCount"].Value;
}
}
if (arg is IFormattable) return ((IFormattable)arg).ToString(format, CultureInfo.CurrentCulture);
if (arg != null) return arg.ToString();
return String.Empty;
}
}
結果如下:
Console.WriteLine(String.Format(cf, "{0:F_4}", 1.35678)); // 1.3568
Console.WriteLine(String.Format(cf, "{0:F_4}", 1.35)); // 1.3500
Console.WriteLine(String.Format(cf, "{0:F_4}", 1.00)); // 1
嘗試這個:
string.Format("{0:#.0000}", Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Price")))
#
代表任何數字,如果沒有數字或數字本身,則0
代表0
。
在MSDN上有關.NET中自定義數字格式的更多信息 。
不幸的是,如果您想為整數隱藏零, 這種方法將行不通 。
設置固定數量的小數位
這與上面的示例相似,但是在我們的格式字符串中,我們使用了哈希('#')
如下所示使用零('0'):string.Format("{0:0.00}", 256.583); // "256.58" string.Format("{0:0.00}", 256.586); // "256.59" string.Format("{0:0.00}", 256.58); // "256.58" string.Format("{0:0.00}", 256.5); // "256.50" string.Format("{0:0.00}", 256.0); // "256.00" - No luck here
更新 :
就像我說的那樣,如果數字在小數部分中沒有數字,這將不會顯示不顯示前導零,因此您可以在格式化之前使用Math.Truncate
方法進行檢查:
var decimalValue = Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Price"));
string.Format((decimalValue - Math.Truncate(decimalValue)) < 0.0001 ? "{0:#.####}" : {0:#.0000}", decimalValue)
但這將為您的程序提供非常大的開銷。
它很簡單
Math.Round(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "Price")), 4).ToString("N4")
控制台樣本
static void Main(string[] args)
{
Console.WriteLine(Math.Round(1122.454540000, 4).ToString("N4")); // output 1122.4545
Console.WriteLine(Math.Round(1122.000100, 4).ToString("N4")); // output 1122.0001
Console.WriteLine(Math.Round(9876.000000, 4).ToString("N4"));// output 9876.0000
Console.WriteLine(Math.Round(1122.4000, 4).ToString("N4"));// output 1122.4000
Console.WriteLine(Math.Round(1122.5400, 4).ToString("N4"));// output 1122.5400
Console.ReadLine();
}
double price= 12.00300;
string op = null;
price= Double.Parse(string.Format("{0:0.0000}", value));
if ((price % 1) != 0)
op = string.Format("{0:#.0000}", price);
else
op = string.Format("{0}", price);
您可以像實用程序功能一樣使用它,也可以根據需要進行更改。
您可以檢查小數點是否為整數,然后使用適當的格式。 具有擴展方法的實現:
public static class DecimalExtension
{
public static string MyFormat(this decimal value)
{
decimal rounded = Math.Round(value, 4);
if ((rounded % 1) == 0)
{
return rounded.ToString("0:0");
}
return rounded.ToString("0:0.0000");
}
}
您可能需要檢查一下: http : //www.daveoncsharp.com/2009/09/formatting-decimals-in-csharp/
特別:
string.Format("{0:0.00}", 256.0); // "256.00"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.