最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

math - parseDecimal in JavaScript? - Stack Overflow

programmeradmin7浏览0评论

I have some JS calculations going on. Since floating point arithmetic often uses close approximations to numbers instead of exact values, if you round these floating point number to fixed precision, you often get slight differences. When you are dealing with dollars, people don't like these slight differences.

My problem is outlined in this jsfiddle: /.

Does anyone know how I can do math operations (multiplication and addition) without introducing these rounding errors ing from the floating point approximations?

EDIT

I found this other post which brings up the same problem, and confirms that JS does not have a built in decimal data type: How to deal with floating point number precision in JavaScript?.

you can see if you JS terminal that

626.175.toFixed(2) == 626.17
626.185.toFixed(2) == 626.18
626.195.toFixed(2) == 626.20

which is inconsistent. We need a true decimal data type.

I have some JS calculations going on. Since floating point arithmetic often uses close approximations to numbers instead of exact values, if you round these floating point number to fixed precision, you often get slight differences. When you are dealing with dollars, people don't like these slight differences.

My problem is outlined in this jsfiddle: http://jsfiddle/rGP8Q/.

Does anyone know how I can do math operations (multiplication and addition) without introducing these rounding errors ing from the floating point approximations?

EDIT

I found this other post which brings up the same problem, and confirms that JS does not have a built in decimal data type: How to deal with floating point number precision in JavaScript?.

you can see if you JS terminal that

626.175.toFixed(2) == 626.17
626.185.toFixed(2) == 626.18
626.195.toFixed(2) == 626.20

which is inconsistent. We need a true decimal data type.

Share Improve this question edited May 23, 2017 at 11:52 CommunityBot 11 silver badge asked Jun 11, 2012 at 17:25 jeffery_the_windjeffery_the_wind 18.3k35 gold badges106 silver badges165 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Yes. Always, ALWAYS deal in units of the smallest denomination, and ONLY divide by 100 at the end, for display purposes only.

发布评论

评论列表(0)

  1. 暂无评论