我需要解决与双重表示相关的问题。 例如,我有以下一组值:
id: 1 val: 8.11 floor: 8.109999 ceil: 8.11
id: 2 val: 8.31 floor: 8.31 ceil: 8.310001
id: 3 val: 8.27 floor: 8.27 ceil: 8.27
id: 4 val: 12.469999999999999 floor: 12.469999 ceil: 12.47
id: 5 val: 1.9700000000000002 floor: 1.97 ceil: 1.970001
其中
val = value coming from a certain operation,
value_ceil = Math.ceil(value * 1000000)/1000000;
value_floor = Math.floor(value * 1000000)/1000000;
正如您所看到的,有不同的情景"这符合我的需求:
* when the initial value is "ready" (= as expected):
- all of them give me the desired output (id 3)
- val and floor give me the desired output (id 2)
- val and ceil give me the desired output (id 1)
* when the initial value has to be "adjusted":
- floor provides the desired output (id 5)
- ceil provides the desired output (id 4 )
是否有一种独特的方法来管理获得所需双值的所有案例?
由于
编辑:可以
Math.round(value * 1000000.0)/1000000.0
解决问题?
答案 0 :(得分:0)
在 java之后,你将放在 ...
这样做:
value_ceil = Math.ceil((value * 1000000)/1000000);
value_floor = Math.floor((value * 1000000)/1000000);
或者如果您想要这样的话:
val:12.190000000000001 floor:12.19 ceil:12.2
然后这应该有效:
double val = 12.190000000000001;
double value_ceil = Math.floor(val*100)/100;
double value_floor = Math.ceil(val*100)/100;