jQuery round decimal to .49或.99 [英] jQuery round decimal to .49 or .99

查看:76
本文介绍了jQuery round decimal to .49或.99的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试研究如何将小数舍入为.49或.99。

I'm trying to work out how to round a decimal to .49 or .99.

我找到 toFixed(2 )功能,但不确定如何向上或向下舍入。

I have found the toFixed(2) function, but not sure how to round up or down.

基本上需要到达最接近的价格点,例如X. 55将下降到X.49,X.84将上升到X.99。

Basically need to get to the closest price point, so for example X.55 would go down to X.49 and X.84 would go up to X.99.

推荐答案

这不需要jQuery但可以用纯JavaScript完成:

This doesn’t require jQuery but can be done with plain JavaScript:

Math.round(price*2)/2 - 0.01

注意还要考虑数字将四舍五入为0的情况(价格> 0.25)因为在这种情况下会产生-0.01。

Note to also consider the case where the number would get rounded to 0 (price > 0.25) as that would yield -0.01 in this case.

这篇关于jQuery round decimal to .49或.99的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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