截断十进制数不能四舍五入四舍五入、十进制数

由网友(花漾)分享简介:可能重复: c# - 我该如何圆一个十进制值2小数位(输出页) 我要截断小数像下面即2.22939393 - > 2.229 2.22977777 - > 2.229 解决方案 双D = 2.22977777;D =((双)((INT)(D * 1000.0)))/ 1000.0;当然,这不会,如果你试图截断...

可能重复:   c# - 我该如何圆一个十进制值2小数位(输出页)

我要截断小数像下面

2.22939393 - > 2.229 2.22977777 - > 2.229 解决方案

 双D = 2.22977777;
D =((双)((INT)(D * 1000.0)))/ 1000.0;
 

当然,这不会,如果你试图截断舍入误差的工作,但它应该正常工作与你给你的例子中的值。参见前两个答案这个问题的详细信息,为什么它不会有时工作。

Possible Duplicate: c# - How do I round a decimal value to 2 decimal places (for output on a page)

C 实现十进制数转二进制数程序

I want to truncate the decimals like below

i.e.

2.22939393 -> 2.229 2.22977777 -> 2.229

解决方案

double d = 2.22977777;
d = ( (double) ( (int) (d * 1000.0) ) ) / 1000.0 ;

Of course, this won't work if you're trying to truncate rounding error, but it should work fine with the values you give in your examples. See the first two answers to this question for details on why it won't work sometimes.

阅读全文

相关推荐

最新文章