取小数后零后面两位数 例如,0.000000111111,我取0.00000011怎么取? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如何取非零部分的两位,把非零部分转换为int型,然后和原来的string比较长度,假设长度少了A,那int转string截取2位,前面加A个0就行了 string str = "0.000000111111"; str = str.Substring(0, str.LastIndexOf("0") + 3); Console.WriteLine(str); decimal d = 0.000000111111m; string temp = Regex.Replace(d.ToString(), @"(?<=[^0.]{2}).+", ""); decimal res = Convert.ToDecimal(temp); Console.WriteLine(res.ToString()); 数值运算,性能更好。试试这个:帮你四舍五入。 double x = 0.000111111; double tx = x; int n = 0; while (tx<1) { tx *= 10; n++; } Console.WriteLine(Math.Round(x, n + 1)); double a = 0.000000111111;string b = a.ToString("0.00000000"); string str = num.ToString(); str = str.Substring(0, str.LastIndexOf("0") + 3);double dLast = str.ToDouble(); 上面我发的错了,要是0.0000011011的情况就有问题9楼的代码不错,稍微改一下double x = 0.000111111;万一是2.00000011呢 double tx = x; int n = 0; while (tx * 10 % 10 == 0)//取出小数位 { tx *= 10; n++; } Console.WriteLine(Math.Round(x, n + 1)); decimal dc=2.1546541641646dc.stostring("f2")好象是这样了. double d = 0.000000234210000001111;double tmp = d - (long)d;int digit = 0;while (tmp < 10){ tmp *= 10; digit++;}double result = Math.Round(d, digit); 正则的反向预搜索是个好法子,只可惜如果是double类型,ToString()的结果不好预测. double a=0.000000111111;string values=string.Format("{0:F8}",a); 0.000000111111这个数字在A1单元格则公式=LEFT(A1,10)得出的数字是10位 C# 会员系统并发控制 求指导 在LABEL的时候运行可以,但是在textbox中就不可以了 从哪儿能看到Console.WriteLine()打印出的数据 timer怎么写?? 菜鸟喊救命:无法创建挂钩! 推荐, .net 如何手动输入进程名称,实现对cpu、内存占用值的输出 请高手回答:有关.net的remoting 编程 Socket和数据库 EF用导航熟悉遍历从表时,删除主表出错 安装visual studio2008哪个版本 输出excel页面自动关闭,无提示“打开”“保存”“取消”对话框
string str = "0.000000111111";
str = str.Substring(0, str.LastIndexOf("0") + 3);
Console.WriteLine(str);
decimal d = 0.000000111111m;
string temp = Regex.Replace(d.ToString(), @"(?<=[^0.]{2}).+", ""); decimal res = Convert.ToDecimal(temp); Console.WriteLine(res.ToString());
double x = 0.000111111;
double tx = x;
int n = 0;
while (tx<1)
{
tx *= 10;
n++;
}
Console.WriteLine(Math.Round(x, n + 1));
string b = a.ToString("0.00000000");
str = str.Substring(0, str.LastIndexOf("0") + 3);
double dLast = str.ToDouble();
9楼的代码不错,稍微改一下
double x = 0.000111111;万一是2.00000011呢
double tx = x;
int n = 0;
while (tx * 10 % 10 == 0)//取出小数位
{
tx *= 10;
n++;
}
Console.WriteLine(Math.Round(x, n + 1));
dc.stostring("f2")
好象是这样了.
double tmp = d - (long)d;
int digit = 0;
while (tmp < 10)
{
tmp *= 10;
digit++;
}
double result = Math.Round(d, digit);
正则的反向预搜索是个好法子,只可惜如果是double类型,ToString()的结果不好预测.
double a=0.000000111111;
string values=string.Format("{0:F8}",a);