jQuery 将小数四舍五入为 0.49 或 0.99

发布于 2024-10-26 03:05:06 字数 137 浏览 3 评论 0原文

我正在尝试弄清楚如何将小数四舍五入为 0.49 或 0.99。

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暗喜 2024-11-02 03:05:06

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

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

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

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

Math.round(price*2)/2 - 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.

倦话 2024-11-02 03:05:06

如果每次 jQuery 让事情变得比他们需要的更加迟钝时我都能得到一美元......

window.round = function(num) {
    cents = (num * 100) % 100;
    if (cents >= 25 && cents < 75) {
        //round x.25 to x.74 -> x.49
        return Math.floor(num) + 0.49;
    }
    if (cents < 25) {
        //round x.00 to x.24 -> [x - 1].99
        return Math.floor(num) - 0.01;
    }
    //round x.75 to x.99 -> x.99
    return Math.floor(num) + 0.99;
};

If I had a dollar for every time jQuery made things more obtuse than they needed to be...

window.round = function(num) {
    cents = (num * 100) % 100;
    if (cents >= 25 && cents < 75) {
        //round x.25 to x.74 -> x.49
        return Math.floor(num) + 0.49;
    }
    if (cents < 25) {
        //round x.00 to x.24 -> [x - 1].99
        return Math.floor(num) - 0.01;
    }
    //round x.75 to x.99 -> x.99
    return Math.floor(num) + 0.99;
};
一抹微笑 2024-11-02 03:05:06

我认为您无法四舍五入/固定为特定数字,您需要检查/计算该值,这可能意味着:四舍五入然后减去 1 或四舍五入并减去 51。

I thing you cant round/fix to a specific number, you will need to check/caclulate the value, which could mean: rounding up then subtracting 1 or rounding up and subtracting 51.

还给你自由 2024-11-02 03:05:06

这不需要 jQuery。你只需要 JavaScript 中的 Math 类,四舍五入还需要一些额外的减法,因为四舍五入会给出最接近的小数

This does not require jQuery. You just need Math class from javascript also rounding off will require some addtional subtraction since rounding will give nearest decimal

七秒鱼° 2024-11-02 03:05:06

稍微编辑一下 aroth 的答案,以便我们还需要四舍五入到最近的 5 乘法值

window.round = function(num) {
cents = (num * 100) % 100;
if (cents >= 25 && cents < 75) {
    //round x.25 to x.74 -> x.49
    return Math.ceil(num/5)*5  + 0.49;
}
if (cents < 25) {
    //round x.00 to x.24 -> [x - 1].99
    return Math.ceil(num/5)*5  - 0.01;
}
//round x.75 to x.99 -> x.99
return Math.ceil(num/5)*5  + 0.99;
};

Editing aroth's answer a little bit in order to lets say we also need to round to nearest 5 multiply value

window.round = function(num) {
cents = (num * 100) % 100;
if (cents >= 25 && cents < 75) {
    //round x.25 to x.74 -> x.49
    return Math.ceil(num/5)*5  + 0.49;
}
if (cents < 25) {
    //round x.00 to x.24 -> [x - 1].99
    return Math.ceil(num/5)*5  - 0.01;
}
//round x.75 to x.99 -> x.99
return Math.ceil(num/5)*5  + 0.99;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文