简体   繁体   English

使用自定义舍入和千位分隔符打印双精度

[英]print double with custom rounding and thousand separator

I've a lot of number to format and I would like them to be readable easily but my users. 我要格式化的数字很多,但我希望用户能轻松阅读它们。 I need to have: 我需要:

  • Custom rounding values (from 0 to 10) 自定义舍入值(从0到10)
  • Thousand Separator 千位分隔符
  • Less garbage 减少垃圾
  • fast processing (update rate : ~10hz) 快速处理(更新速度:〜10hz)

The best I came up with is this code: 我想到的最好的是此代码:

private string RoundToString(double d)
{
    return Double.IsNaN(d) ? "Out Of Bounds" : d.ToString("N" + (int)numUpDownRoundValue.Value);
}

but I get a lot of useless 0 if the rounding is high: 但是如果舍入很高,我会得到很多无用的0

182,0000000000  370,5000000000          370,5444319880  121,0000000000  517,0000000000  5 949 090,8555680300                        
105,3700000000  158,2750000000          165,4869516069  74,1850000000   245,4200000000  2 656 893,0080484100            
107 277,0000000000  109 452,0000000000          110 879,4795092180  100 261,0000000000  119 221,0000000000  1 780 170 043,5204900000

I also try this one: 我也尝试这个:

private string RoundToString(double d)
{
    return Double.IsNaN(d) ? "Out Of Bounds" : Math.Round(d, (int)numUpDownRoundValue.Value).ToString("N");
}

But the N option crop to early (loss of precision) 但是N选件会提前播种(精度下降)

Edit 编辑

What I want : 我想要的是 :

182 370,5 -- 370,5444319880 -- 121 517 -- 5 949 090,85556803                    
105,37 -- 158,275 -- 165,4869516069 -- 74,185 -- 245,42 -- 2 656 893,00804841           
107 277 -- 109 452 -- 110 879,479509218 -- 100 261  119 221 -- 1 780 170 043,52049

In other words, I want thousand separator and no useless zeros. 换句话说,我想要千位分隔符,并且没有无用的零。

如果您希望小数点后最多 10位,但又不想训练零,请使用:

d.ToString("#,##0."+new string('#',(int)numUpDownRoundValue.Value))

What about: 关于什么:

d.ToString("#,#.##########")

Look up custom numeric format on the msdn site. 在msdn站点上查找自定义数字格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM