winform c# 验证数字 精度 有效位 求 c# 验证精度 有效位,比如,我要4个有效位,精度3,那么,689.555 -> 689.5 2.98545 -> 2.985有效位的重要性大于精度.即以有效位为主. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 N次乘以10 取整,再N次除以10,N就是精度 #region 验证精度和有效位 /// <summary> /// 验证精度和有效位 /// </summary> /// <param name="value">原数字</param> /// <param name="ableSize">有效位</param> /// <param name="precision">精度</param> /// <returns></returns> public static double CheckValuePrecision(string inputdata, int ableSize, int precision) { int k = inputdata.IndexOf("."); string str1 = string.Empty; if (k > 0) { str1 = inputdata.Substring(0, k); } else { str1 = inputdata; } string str2 = inputdata.Substring(inputdata.IndexOf(".") + 1); string factor = string.Empty; for (int i = 0; i < ableSize; i++) { factor += "9"; } if (str1.Length > ableSize) { return Convert.ToDouble(factor); } else if (str1.Length == ableSize) { return Convert.ToDouble(str1); } else { int cutLength = 0; if (str1 == "0") { cutLength = ableSize; } else { cutLength = ableSize - str1.Length; } if (str2.Length >= cutLength) { return Convert.ToDouble(str1 + "." + str2.Substring(0, cutLength)); } else { return Convert.ToDouble(str1 + "." + str2); } } } #endregion 帮忙解释一下为什么是这个输出结果? C# 回调函数 线程问题 C# 自动更新程序 带CRC校验的 请问高手,你们用c#都开发什么类型的程序?? 全文索引问题 fastreport可以放在.net的控件集里吗,以后就可以像用控件那样的拖到窗体里 怎么样固定datagrid的宽度,急! C#读取Excel内容(包含日文),提示外部表不是预期的格式 求C#贪吃蛇游戏源程序 订单逾期如何检查 c#使用StreamWriter导出DataGridView数据到excel中的格式问题 超级终端-AT指令怎样清屏?
/// <summary>
/// 验证精度和有效位
/// </summary>
/// <param name="value">原数字</param>
/// <param name="ableSize">有效位</param>
/// <param name="precision">精度</param>
/// <returns></returns>
public static double CheckValuePrecision(string inputdata, int ableSize, int precision)
{
int k = inputdata.IndexOf(".");
string str1 = string.Empty;
if (k > 0)
{
str1 = inputdata.Substring(0, k);
}
else
{
str1 = inputdata;
}
string str2 = inputdata.Substring(inputdata.IndexOf(".") + 1); string factor = string.Empty;
for (int i = 0; i < ableSize; i++)
{
factor += "9";
} if (str1.Length > ableSize)
{
return Convert.ToDouble(factor);
}
else if (str1.Length == ableSize)
{
return Convert.ToDouble(str1);
}
else
{
int cutLength = 0;
if (str1 == "0")
{
cutLength = ableSize;
}
else
{
cutLength = ableSize - str1.Length;
}
if (str2.Length >= cutLength)
{
return Convert.ToDouble(str1 + "." + str2.Substring(0, cutLength));
}
else
{
return Convert.ToDouble(str1 + "." + str2);
}
}
}
#endregion