使用双倍至两位小数位 [英] Using double up to two decimal places

查看:147
本文介绍了使用双倍至两位小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用double,最多只能使用2位小数。即它将被存储高达2位小数,如果比较两个双重值,则比较应仅基于前2位小数。如何实现这样的事情?我的意思是存储,比较,一切都将只是基于小数点的前两位。其余地方可能不同,大于,小于,无关紧要。

I want to use double up to just 2 decimal places. i.e. it will be stored upto 2 decimal places, if two double values are compared then the comparison should be based on only the first 2 decimal places. How to achieve such a thing? I mean storing, comparison, everything will be just based on the 1st two decimal places. The remaining places may be different, greater than, less than, doesn't matter.

编辑
我的值不大。说从0到5000最大。但是我必须乘以Cos A,Sin A多次,在程序中A的值不断变化。

EDIT My values arent large. say from 0 to 5000 maximum. But I have to multiply by Cos A, Sin A a lot of times, where the value of A keeps changing during the course of the program.

编辑
看看我的程序,一辆汽车以特定的速度移动,比如12米/秒。几分钟后,车子就会改变方向,就像选择一个新的角度一样,沿着这个方向开始沿直线移动。现在每次移动,我必须在地图上找出它的x和y位置。这将是currentX + velocity * Cos A和currentY + Velocity * Sin A.但是由于这种情况经常发生,所以随着时间的推移会有很多累积误差。如何避免这种情况?

EDIT Look in my program a car is moving at a particular speed, say 12 m/s. Now after every few minutes, the car changes direction, as in chooses a new angle and starts moving in a straight line along that direction. Now everytime it moves, I have to find out its x and y position on the map. which will be currentX+velocity*Cos A and currentY+Velocity*Sin A. but since this happens often, there will be a lot of cumulative error over time. How to avoid that?

推荐答案

比较浮点值的相等值应始终使用某种形式的delta / epsilon比较:

Comparing floating point values for equality should always use some form of delta/epsilon comparison:

if (Abs(value1 - value2) < 0.01 )
{
   // considered equal to 2 decimal places
}

这篇关于使用双倍至两位小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆