我想一个函数,如下所示:
I'd like a function that looks like this:
int GetDecimalPlaces(string format, IFormatProvider formatProvider = null);
的投入将是完全一样的,可依法传递给负责格式化数字,例如, double.ToString
,十进制方法的ToString
。
将输出一个 INT
表示最低数量由格式字符串所需的小数位。
The output would be an int
indicating the lowest number of decimal places required by the format string.
因此,这里有几个例子输入/输出我希望(我们只能说离开 formatProvider
为空
结果在目前的文化正在使用):
So here are a couple of example inputs/outputs I would expect (let's just say leaving formatProvider
as null
results in the current culture being used):
Input | Output
------|-------
N2 | 2
0 | 0
0.000 | 3
g | 0
0.0## | 1
如果可能的话,我想这样做的正确的方式;即,没有黑客。但是,如果黑客必须这样,我也要AP preciate的好的黑客的建议;)
If possible, I'd like to do this the "right" way; i.e., without hacks. But if hack I must, I would also appreciate good hacking suggestions ;)
推荐答案
也许最简单的方法是采取一个整数,如1,并格式化,然后解析字符串来算的小数位数。我想这有资格作为一个黑客,但它应该工作pretty的可靠。
Probably the easiest way would be to take a whole number, like 1, and format it, then parse the string to count the number of decimal places. I guess this qualifies as a hack, but it should work pretty reliably.
相关推荐
最新文章