Does anyone know how I can get a format string to use bankers rounding? I have been using "{0:c}" but that doesn't round the same way that bankers rounding does. The Math.Round()
method does bankers rounding. I just need to be able to duplicate how it rounds using a format string.
有谁知道如何使用格式字符串来使用银行家舍入?我一直在使用“{0:c}”,但这并不像银行家四舍五入那样。 Math.Round()方法对银行家进行舍入。我只需要能够使用格式字符串复制它的舍入方式。
Note: the original question was rather misleading, and answers mentioning regex derive from that.
注意:原始问题相当误导,提到正则表达式的答案来自于此。
5 个解决方案
#1
3
Can't you simply call Math.Round() on the string input to get the behavior you want?
你不能简单地在字符串输入上调用Math.Round()来获得你想要的行为吗?
Instead of:
string s = string.Format("{0:c}", 12345.6789);
Do:
string s = string.Format("{0:c}", Math.Round(12345.6789));
#2
3
Regexp is a pattern matching language. You can't do arithmetic operations in Regexp.
Regexp是一种模式匹配语言。你不能在Regexp中进行算术运算。
Do some experiements with IFormatProvider and ICustomFormatter. Here is a link might point you in the right direction. http://codebetter.com/blogs/david.hayden/archive/2006/03/12/140732.aspx
使用IFormatProvider和ICustomFormatter进行一些实验。这是一个链接可能会指向正确的方向。 http://codebetter.com/blogs/david.hayden/archive/2006/03/12/140732.aspx
#3
0
Its not possible, a regular expression doesn't have any concept of "numbers". You could use a match evaluator but you'd be adding imperative c# code, and would stray from your regex only requirement.
它是不可能的,正则表达式没有任何“数字”的概念。您可以使用匹配评估程序,但是您将添加命令式c#代码,并且会偏离您的正则表达式要求。
#4
0
.Net has built in support for both Arithmetic and Bankers' rounding:
.Net已经建立了对算术和银行家四舍五入的支持:
//midpoint always goes 'up': 2.5 -> 3
Math.Round( input, MidpointRounding.AwayFromZero );
//midpoint always goes to nearest even: 2.5 -> 2, 5.5 -> 6
//aka bankers' rounding
Math.Round( input, MidpointRounding.ToEven );
"To even" rounding is actually the default, even though "away from zero" is what you learnt at school.
“甚至”舍入实际上是默认的,即使“远离零”是你在学校学到的东西。
This is because under the hood computer processors also do bankers' rounding.
这是因为在引擎盖下计算机处理器也做银行家的四舍五入。
//defaults to banker's
Math.Round( input );
I would have thought that any rounding format string would default to bankers' rounding, is this not the case?
我原本以为任何舍入格式字符串都会默认为银行家的舍入,是不是这样呢?
#5
0
If you are using .NET 3.5, you can define an extension method to help you do this:
如果您使用的是.NET 3.5,则可以定义一个扩展方法来帮助您执行此操作:
public static class DoubleExtensions
{
public static string Format(this double d)
{
return String.Format("{0:c}", Math.Round(d));
}
}
Then, when you call it, you can do:
然后,当你打电话时,你可以这样做:
12345.6789.Format();
#1
3
Can't you simply call Math.Round() on the string input to get the behavior you want?
你不能简单地在字符串输入上调用Math.Round()来获得你想要的行为吗?
Instead of:
string s = string.Format("{0:c}", 12345.6789);
Do:
string s = string.Format("{0:c}", Math.Round(12345.6789));
#2
3
Regexp is a pattern matching language. You can't do arithmetic operations in Regexp.
Regexp是一种模式匹配语言。你不能在Regexp中进行算术运算。
Do some experiements with IFormatProvider and ICustomFormatter. Here is a link might point you in the right direction. http://codebetter.com/blogs/david.hayden/archive/2006/03/12/140732.aspx
使用IFormatProvider和ICustomFormatter进行一些实验。这是一个链接可能会指向正确的方向。 http://codebetter.com/blogs/david.hayden/archive/2006/03/12/140732.aspx
#3
0
Its not possible, a regular expression doesn't have any concept of "numbers". You could use a match evaluator but you'd be adding imperative c# code, and would stray from your regex only requirement.
它是不可能的,正则表达式没有任何“数字”的概念。您可以使用匹配评估程序,但是您将添加命令式c#代码,并且会偏离您的正则表达式要求。
#4
0
.Net has built in support for both Arithmetic and Bankers' rounding:
.Net已经建立了对算术和银行家四舍五入的支持:
//midpoint always goes 'up': 2.5 -> 3
Math.Round( input, MidpointRounding.AwayFromZero );
//midpoint always goes to nearest even: 2.5 -> 2, 5.5 -> 6
//aka bankers' rounding
Math.Round( input, MidpointRounding.ToEven );
"To even" rounding is actually the default, even though "away from zero" is what you learnt at school.
“甚至”舍入实际上是默认的,即使“远离零”是你在学校学到的东西。
This is because under the hood computer processors also do bankers' rounding.
这是因为在引擎盖下计算机处理器也做银行家的四舍五入。
//defaults to banker's
Math.Round( input );
I would have thought that any rounding format string would default to bankers' rounding, is this not the case?
我原本以为任何舍入格式字符串都会默认为银行家的舍入,是不是这样呢?
#5
0
If you are using .NET 3.5, you can define an extension method to help you do this:
如果您使用的是.NET 3.5,则可以定义一个扩展方法来帮助您执行此操作:
public static class DoubleExtensions
{
public static string Format(this double d)
{
return String.Format("{0:c}", Math.Round(d));
}
}
Then, when you call it, you can do:
然后,当你打电话时,你可以这样做:
12345.6789.Format();