在 C# 中实现复杂数字格式的最佳方法 [英] Best way to achieve complicated numeric formatting in C#
问题描述
在 C# 中复制此类行为的最佳方法是什么?
What is the best way to replicate the behaviour of something like this in C#?
// Converts decimal to a 0-padded string with minimum specified width and precision.
sprintf(out, "%0*.*lf", width, precision, decimal);
我阅读了标准、自定义 和 composite 格式字符串,但我看不出有什么好的方法可以实现这一点.
I had a read of standard, custom and composite format strings, but I can't see any nice way to achieve this.
我问的原因是我刚刚在我维护的一些代码中遇到了这个丑陋的片段,我们需要根据外部接口指定的可变宽度和精度来格式化一堆小数:
The reason I ask is that I just came across this ugly snippet in some code I am maintaining where we are required to format a bunch of decimals according to variable widths and precisions as specified by an external interface:
private static String ZEROS = "00000000000000000000000000";
public override string Format(decimal input, int width, int precision)
{
String formatString = ZEROS.Substring(0, width - precision - 1)
+ "." + ZEROS.Substring(0, precision);
return ToDecimal(input).ToString(formatString);
}
我想用一些不那么可怕的东西来代替它.
and I would like to replace it with someing a little less horrendous.
更新
由于最终答案隐藏在评论中,所以这里是:
Since the final answer is buried in comments, here it is:
public override string Format(decimal input, int width, int precision)
{
string.Format("{0," + width + ":F" + precision + "}", input).Replace(" ","0");
}
在我的情况下,输入总是正的,所以这也有效:
In my case the input is always positive so this works too:
public override string Format(decimal input, int width, int precision)
{
return input.ToString("F"+precision).PadLeft(width, 0);
}
推荐答案
你可以这样写,生成一个标准格式的字符串然后使用它:
You could write it like this, generating a standard format string and then using it:
public static string Format(decimal input, int width, int precision)
{
var format = string.Format("{{0,{0}:F{1}}}", width, precision);
// generates (e.g. width=15, precision=5) "{0,15:F5}"
// then format using this and replace padding with zeroes
return string.Format(format, input).Replace(" ", "0");
}
或者不是两次调用 format 而是连接格式字符串,这取决于您的偏好:
Or instead of calling format twice just concat the format string, depending on your preference:
string.Format("{0," + width + ":F" + precision + "}", input).Replace(" ","0")
之后您必须替换空格,因为无法指定填充字符.或者,您可以编写自己的格式化程序来填充特定字符.这虽然有效:)
You have to replace the spaces afterwards since there's no way to specify the padding character. Alternately you could write your own formatter to pad with a specific character. This works though :)
这与所有输入的原始匹配,precision = 0
除外.在这种情况下,原始数据是不正确的,因为即使小数点不存在,它也会将小数点计为宽度的一部分.
This matches the original for all inputs except when precision = 0
. In that case, the original is incorrect, since it counts the decimal point as part of the width even when it isn't present.
糟糕:忘记检查负数.
这是一个更简单的版本,但必须检查数字是否为负数才能使填充正确:
Here is a simpler version, but have to check if the number is negative to get the padding correct:
public override string Format(decimal input, int width, int precision)
{
var output = input.ToString("F" + precision);
return input < 0
? "-" + output.Substring(1).PadLeft(width, '0')
: output .PadLeft(width, '0');
}
这篇关于在 C# 中实现复杂数字格式的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!