我的结果应该是这样的:
double n1 = 0.024862;
double n2 = 1357512.15;
double n3 = 124.187;
Output:
N1: 0.0248
N2: 1,357,512.15
N3: 124.18
这就是我现在所拥有的:
n1.ToString("#,0.00", CultureInfo.InvariantCulture);
适用于大数字,但如果我有一个像0.0005524这样的数字我会得到0.00而我想要0.000552
第一次出现数字后的2位数
答案 0 :(得分:1)
不确定这是否是您想要的:
private static string NFiguresAfterFirstDecimal(double d, int figures)
{
int c = 0;
while (Math.Abs(d) < 1.0 && Math.Abs(d) > 1E-50){
d *= 10.0;
c++;
}
d = Math.Round(d, figures) / Math.Pow(10.0, c);
if (Math.Abs(d) < 1.0 && c > 2)
return ((decimal) d).ToString();
else
return d.ToString("#,0.00", CultureInfo.InvariantCulture);
}
试运行:
double[] list = {0.00055243435, 0.00, 0.1, 1357512.157, 1.0001, 1.1, 123.445};
foreach(double d in list)
Console.WriteLine(NFiguresAfterFirstDecimal(d,2));
输出:
0.000552
0.00
0.10
1,357,512.16
1.00
1.10
123.44