如何拆分双号。 [英] How to split double number.
本文介绍了如何拆分双号。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的双号:
double inputValue = 48.4866666;
我想:inputValue = 48.48
inputValue = Math.Round(inputValue,2);
我试过的:
inputValue = Math.Round(inputValue,2);但它给48.49我需要48.48
My double number:
double inputValue = 48.4866666;
I want: inputValue = 48.48
inputValue = Math.Round(inputValue, 2);
What I have tried:
inputValue = Math.Round(inputValue, 2); but its give 48.49 and I need 48.48
推荐答案
检查这个类似的线程与解决的答案
c# - 获取没有舍入的最后2个小数位数 [ ^ ]
check this similar thread with Solved Answers
c# - Get Last 2 Decimal Places with No Rounding [^]
使用 Math.Truncate Method(Double)(System) [ ^ ]
Use Math.Truncate Method (Double) (System)[^]
inputValue = Math.Truncate(inputNumber * 100.0) / 100.0;
Round(Double,Int32)
正在四舍五入到休息。如果您需要不同的舍入模式,请使用 Math.Round方法(Double,Int32, MidpointRounding)(系统) [ ^ ]。
在你的情况下:
Round(Double, Int32)
is rounding to nearest. If you need a different rounding mode use Math.Round Method (Double, Int32, MidpointRounding) (System)[^].
In your case:
inputValue = Math.Round(inputValue, 2, MidpointRounding.AwayFromZero);
如果您知道输入值始终有效且为正,则您还可以添加一半的舍入位置值:
If you know that the input value is always valid and positive, you may also add half of the rounding position value:
inputValue = Math.Round(inputValue + 0.005, 2);
对于负数,您必须减去。
For negative numbers you have to subtract.
这篇关于如何拆分双号。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文