C#的话用“G"的精度表示保留有效数字位数,例如保留4位var arr = new double[] { 123.4567, 12345670, 12.34567, 0.001234567 };
foreach (var d in arr)
Console.WriteLine(d.ToString("G4"));//数据
123.5
1.235E+07
12.35
0.001235
foreach (var d in arr)
Console.WriteLine(d.ToString("G4"));//数据
123.5
1.235E+07
12.35
0.001235
解决方案 »
- savafiledialog使用如何在sava的时候文件名默认一个值,比如“ww”
- 如果两进程间需要传输大量byte,应该使用DOTNET那种技术,MessageQueue,remoting socket
- DataGridViewComboBoxColumn为什么要点多下才恩那个选择
- 有些类里面系统自动生成的components.container components是干啥用的,没有搜到
- 图片缩放问题
- 请教个有关datagridview绑定dataset的问题
- 如何在asp和aspx页面间传值或相互调用值呢?
- 为何Debug和Release编译的结果不同?
- tabControl如何能使选项卡排成两层
- Web中操作文件没有权限???
- 请问,如何理解WaitOne方法(Int32, Boolean),退出同步区域?
- C# 串口通信、单片机的似乎很少,我汇总了一些,大家也一起来汇总吧
\d+[\.]\d{几位}
-----------------
static void Main(string[] args)
{
Program p = new Program();
p.aa();
} public void aa()
{
string ss = "123.456";
ss = Regex.Match(ss.ToString(), @"\d+[\.]\d{3}", RegexOptions.IgnoreCase).ToString();
Console.WriteLine(ss);
Console.Read();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double[] data = new double[]
{
123.456,
10.334,
108.251231,
2000.0,
2000123.0,
0.0000123456,
0.000031415926,
0.0,
-0.0,
-0.0007,
-0.0523,
-0.005432354,
-0.9999,
-1.0,
-12.2345,
-100.0,
-12345.2345,
-15555.55555
};
for (int i = 0; i <= 5; i++)
{
string result = string.Join("\r\n", data.Select(x => string.Format("\t{0} -> {1}", x, foo(x, i))));
Console.WriteLine(i.ToString() + ":\r\n" + result + "\r\n");
}
} static double foo(double d, int n)
{
if (d == 0.0) return 0;
if (d > 1 || d < -1)
n = n - (int)Math.Log10(Math.Abs(d)) - 1;
else
n = n + (int)Math.Log10(1.0 / Math.Abs(d));
if (n < 0)
{
d = (int)(d / Math.Pow(10, 0 - n)) * Math.Pow(10, 0 - n);
n = 0;
}
return Math.Round(d, n);
}
}
}
0:
123.456 -> 0
10.334 -> 0
108.251231 -> 0
2000 -> 0
2000123 -> 0
1.23456E-05 -> 0
3.1415926E-05 -> 0
0 -> 0
0 -> 0
-0.0007 -> -0.001
-0.0523 -> -0.1
-0.005432354 -> -0.01
-0.9999 -> -1
-1 -> -1
-12.2345 -> 0
-100 -> 0
-12345.2345 -> 0
-15555.55555 -> 01:
123.456 -> 100
10.334 -> 10
108.251231 -> 100
2000 -> 2000
2000123 -> 2000000
1.23456E-05 -> 1E-05
3.1415926E-05 -> 3E-05
0 -> 0
0 -> 0
-0.0007 -> -0.0007
-0.0523 -> -0.05
-0.005432354 -> -0.005
-0.9999 -> -1
-1 -> -1
-12.2345 -> -10
-100 -> -100
-12345.2345 -> -10000
-15555.55555 -> -100002:
123.456 -> 120
10.334 -> 10
108.251231 -> 100
2000 -> 2000
2000123 -> 2000000
1.23456E-05 -> 1.2E-05
3.1415926E-05 -> 3.1E-05
0 -> 0
0 -> 0
-0.0007 -> -0.0007
-0.0523 -> -0.052
-0.005432354 -> -0.0054
-0.9999 -> -1
-1 -> -1
-12.2345 -> -12
-100 -> -100
-12345.2345 -> -12000
-15555.55555 -> -150003:
123.456 -> 123
10.334 -> 10.3
108.251231 -> 108
2000 -> 2000
2000123 -> 2000000
1.23456E-05 -> 1.23E-05
3.1415926E-05 -> 3.14E-05
0 -> 0
0 -> 0
-0.0007 -> -0.0007
-0.0523 -> -0.0523
-0.005432354 -> -0.00543
-0.9999 -> -1
-1 -> -1
-12.2345 -> -12.2
-100 -> -100
-12345.2345 -> -12300
-15555.55555 -> -155004:
123.456 -> 123.5
10.334 -> 10.33
108.251231 -> 108.3
2000 -> 2000
2000123 -> 2000000
1.23456E-05 -> 1.235E-05
3.1415926E-05 -> 3.142E-05
0 -> 0
0 -> 0
-0.0007 -> -0.0007
-0.0523 -> -0.0523
-0.005432354 -> -0.005432
-0.9999 -> -0.9999
-1 -> -1
-12.2345 -> -12.23
-100 -> -100
-12345.2345 -> -12340
-15555.55555 -> -155505:
123.456 -> 123.46
10.334 -> 10.334
108.251231 -> 108.25
2000 -> 2000
2000123 -> 2000100
1.23456E-05 -> 1.2346E-05
3.1415926E-05 -> 3.1416E-05
0 -> 0
0 -> 0
-0.0007 -> -0.0007
-0.0523 -> -0.0523
-0.005432354 -> -0.0054324
-0.9999 -> -0.9999
-1 -> -1
-12.2345 -> -12.234
-100 -> -100
-12345.2345 -> -12345
-15555.55555 -> -15556Press any key to continue . . .
public string EffectiveNumber(string value, string precision)
{
Decimal newValue = Convert.ToDecimal(value);
string newStr = newValue.ToString("G" + precision);
string str = Convert.ToDecimal(Double.Parse(newStr)).ToString();
int i = 0;
bool isCheck = false;
if (str.IndexOf(".") > -1)
{
string[] values = str.Split('.');
foreach (string s in values)
{
foreach (char c in s)
{
if (c != '0')
{
isCheck = true;
i++;
}
else
{
if (isCheck == true)
{
i++;
}
}
}
}
}
if (i != 0 && i < Convert.ToDouble(precision))
{
for (int j = i; j < Convert.ToInt32(precision); j++)
{
str += "0";
}
} return str;
}
#endregion