为的ToString奇数小数类型的行为(的IFormatProvider) [英] Odd decimal type behavior for ToString(IFormatProvider)
本文介绍了为的ToString奇数小数类型的行为(的IFormatProvider)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var numberFormat = new NumberFormatInfo();
numberFormat.NumberDecimalSeparator = ".";
numberFormat.NumberDecimalDigits = 2;
decimal a = 10.00M;
decimal b = 10M;
Console.WriteLine(a.ToString(numberFormat));
Console.WriteLine(b.ToString(numberFormat));
Console.WriteLine(a == b ? "True": "False");
在控制台:
10.00
10
真
In console: 10.00 10 True
为什么会不同?更重要的是,我该怎么称呼的ToString()来不管确保相同的输出变量如何初始化?
Why is it different? More important, how do I call ToString() to ensure same output no matter how a variable is initialized?
推荐答案
的 NumberDecimalDigits
属性用于与F
和N
。标准格式字符串,而不是所谓的无格式字符串中的的ToString
法
The NumberDecimalDigits
property is used with the "F"
and "N"
standard format strings, not the ToString
method called without a format string.
您可以使用:
Console.WriteLine(a.ToString("N", numberFormat));
这篇关于为的ToString奇数小数类型的行为(的IFormatProvider)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文