/// <summary> /// Return count of decimals after decimal-place /// </summary> private int getDecimalCount(double val) { int count = 0; ... return count; }
参数’val’是(此方法的正常值的样本)
5900.00返回2
5900.09返回2
5900.000返回3
5900.001返回3
1.0返回1
0.0000005返回7
1.0000000返回7
5900822返回0
我的问题是计算.0 .00 .000等等
题:
我该如何解决这个问题?如果这种方法不可能与双打,怎么回事?
[EDITED]
– 进行小的文本更改以提出更明确的问题,
– 由于’小数位’的错误使用含义导致的语言更正
与@Miguel类似,这是我将如何处理它:
public static int getDecimalCount(double dVal,string sVal,string culture){ CultureInfo info = CultureInfo.GetCultureInfo(culture); //Get the double value of the string representation,keePing culture in mind double test = Convert.Todouble(sVal,info); //Get the decimal separator the specifIEd culture char[] sep = info.NumberFormat.NumberDecimalSeparator.tochararray(); //Check to see if the culture-adjusted string is equal to the double if (dVal != test) { //The string conversion isn't correct,so throw an exception throw new System.ArgumentException("dVal doesn't equal sVal for the specifIEd culture"); } //Split the string on the separator string[] segments = sVal.Split(sep); switch (segments.Length) { //Only one segment,so there was not fractional value - return 0 case 1: return 0; //Two segments,so return the length of the second segment case 2: return segments[1].Length; //More than two segments means it's invalID,so throw an exception default: throw new Exception("Something bad happened!"); }}
美国英语的快捷方法:
public static int getDecimalCount(double dVal,string sVal){ return getDecimalCount(dVal,sVal,"en-US");}
测试:
static voID Main(string[] args){ int i = 0; double d = 5900.00; string s = "5900.00"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",d,s); i = getDecimalCount(d,s); Console.Writeline("Expected output: 2. Actual output: {0}",i); Console.Writeline(); d = 5900.09; s = "5900.09"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",i); Console.Writeline(); d = 5900.000; s = "5900.000"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",s); Console.Writeline("Expected output: 3. Actual output: {0}",i); Console.Writeline(); d = 5900.001; s = "5900.001"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",i); Console.Writeline(); d = 1.0; s = "1.0"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",s); Console.Writeline("Expected output: 1. Actual output: {0}",i); Console.Writeline(); d = 0.0000005; s = "0.0000005"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",s); Console.Writeline("Expected output: 7. Actual output: {0}",i); Console.Writeline(); d = 1.0000000; s = "1.0000000"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",i); Console.Writeline(); d = 5900822; s = "5900822"; Console.Writeline("Testing with dVal = {0} and sVal = {1}.",s); Console.Writeline("Expected output: 0. Actual output: {0}",i); Console.Readline();}
最后,测试的输出:
Testing with dVal = 5900 and sVal = 5900.00.
Expected output: 2. Actual output: 2Testing with dVal = 5900.09 and sVal = 5900.09.
Expected output: 2. Actual output: 2Testing with dVal = 5900 and sVal = 5900.000.
Expected output: 3. Actual output: 3Testing with dVal = 5900.001 and sVal = 5900.001.
Expected output: 3. Actual output: 3Testing with dVal = 1 and sVal = 1.0.
Expected output: 1. Actual output: 1Testing with dVal = 5E-07 and sVal = 0.0000005.
Expected output: 7. Actual output: 7Testing with dVal = 1 and sVal = 1.0000000.
Expected output: 7. Actual output: 7Testing with dVal = 5900822 and sVal = 5900822.
Expected output: 0. Actual output: 0
如果您对此有疑问或者没有意义,请告诉我.
总结以上是内存溢出为你收集整理的c# – 双精度数据类型,小数位后计数小数全部内容,希望文章能够帮你解决c# – 双精度数据类型,小数位后计数小数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)