javascript 数学,以 0.05 为增量四舍五入到小数点后两位

发布于 2024-12-05 14:54:08 字数 558 浏览 2 评论 0原文

我提前道歉。我读过很多帖子,我确信答案在于实现 toFixed() 方法,但不知道如何实现。

$('.addsurcharge').click(function() {               
    $('span.depositamount').text(function(i,v) {
        return Math.round(parseInt(v * 100, 10) * 0.025) / 100 + (v * 1);
    });      
});

$('.nosurcharge').click(function() {            
    $('span.depositamount').text(function(i,v) {
        return Math.round(parseInt(v * 100, 10) * -0.025) / 100 + (v * 1);
    });      
});

基本上,上述内容是为 AMEX 存款添加(或减去)2.5% 的附加费,我希望它四舍五入到最接近的小数点后两位。最好以 0.05 为增量。

帮助表示感谢!

I apologise in advance. I've read thru a number of post and I'm sure the answer lies in implementing the toFixed() method, but can't work out how.

$('.addsurcharge').click(function() {               
    $('span.depositamount').text(function(i,v) {
        return Math.round(parseInt(v * 100, 10) * 0.025) / 100 + (v * 1);
    });      
});

$('.nosurcharge').click(function() {            
    $('span.depositamount').text(function(i,v) {
        return Math.round(parseInt(v * 100, 10) * -0.025) / 100 + (v * 1);
    });      
});

Basically, the above is adding (or subtracting) a 2.5% surcharge for AMEX deposits and I want it to round up to the nearest two decimal amount. Preferably in .05 increments.

Help appreciated!

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

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

发布评论

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

评论(5

仅一夜美梦 2024-12-12 14:54:08

货币最简单的方法是使用小单位,然后四舍五入并仅在最后转换为主要单位。因此,对于包含美元和美分的系统,请以美分计算直到最后,然后转换为美元。如果要输入的文本格式为“2.03”,那么它可以使用以下内容将其四舍五入到最接近的 $0.05:

function roundToFiveCents(v) {

  // Turn dollars into cents and convert to number
  var len;
  v = v * 100;

  // Get residual cents
  var r = v % 5;

  // Round to 5c
  if (r) {
    v -= (r == 1 || r == 2)? r : r-5;
  }

  // Convert to string
  v = v + '';
  len = v.length - 2;

  // Return formatted string
  return v.substring(0, len) + '.' + v.substring(len);
}

这可以更简洁,但我看不到这一点,它只会导致混淆。字符串操作非常快,用简单的加法和减法取代了 JavaScript 小数乘法和除法的不确定性。

例如借用 rfausak 的答案:

// Returns string, remove ' + ''' to return number
function roundToFiveCents(v) {
  return (Math.round(v*20)/20).toFixed(2) + '';
}

The easiest way for currency is to work in the minor units, then round and convert to major units only at the end. So for a system with dollars and cents, work in cents until the end, then convert to dollars. If the text to be input is of the format "2.03", then it can rounded it to the nearest $0.05 using something like:

function roundToFiveCents(v) {

  // Turn dollars into cents and convert to number
  var len;
  v = v * 100;

  // Get residual cents
  var r = v % 5;

  // Round to 5c
  if (r) {
    v -= (r == 1 || r == 2)? r : r-5;
  }

  // Convert to string
  v = v + '';
  len = v.length - 2;

  // Return formatted string
  return v.substring(0, len) + '.' + v.substring(len);
}

That can be more concise, but I can't see the point, it would only server to obfuscate. String manipulation is very fast and replaces the uncertainty of javascript multiplication and division of decimal fractions with simple addition and subtraction.

e.g. borrowing from rfausak's answer:

// Returns string, remove ' + ''' to return number
function roundToFiveCents(v) {
  return (Math.round(v*20)/20).toFixed(2) + '';
}
﹂绝世的画 2024-12-12 14:54:08

如果您的问题是“我如何采用 v(包含美元和美分金额的变量)并应用 2.5% AMEX 附加费,并将结果四舍五入到最接近的美分(即,四舍五入到两分)小数)?”然后您可以尝试以下操作:

return (v * 1.025).toFixed(2);

如果您有可变附加费,请尝试以下操作:

var surcharge = 0.025; // or whatever percentage applies

return (v * (1 + surchage)).toFixed(2);

请注意,toFixed 返回字符串表示形式,但它会为您进行舍入。

(关于您的“无附加费”功能,您无法通过乘以 -0.025 来删除之前应用的 0.025 附加费。您通过乘以 1.025 来应用附加费,因此您可以通过除以 1.025 来删除它。)

If your question is "How do I take v, a variable that contains a dollar-and-cents amount, and apply a 2.5% AMEX surcharge with the result rounded to the nearest cent (i.e., to two decimals)?" Then you can try this:

return (v * 1.025).toFixed(2);

If you have a variable surcharge try this:

var surcharge = 0.025; // or whatever percentage applies

return (v * (1 + surchage)).toFixed(2);

Note that toFixed returns a string representation, but it does the rounding for you.

(Regarding your "nosurcharge" function, you can't remove a 0.025 surcharge that was applied previously by multiplying by -0.025. You apply the surchage by multiplying by 1.025, so you remove it by dividing by 1.025.)

你曾走过我的故事 2024-12-12 14:54:08

虽然帖子有点旧,但问题不是。
我认为海报的意思是,某些货币以美分计算,但价格以 x.00 或 x.05 美分给出。例如瑞士货币。

解决方案借鉴了 RobG 的答案,并做了一些修改:

function roundToFiveCents(v) {
    var len;
    // Format to 2 numbers after decimal point an turn dollars into cents
    v = v.toFixed(2) * 100;

    // Get residual cents
    var r = v % 5;

    // Round to 5c
    if (r) {
        v -= (r === 1 || r === 2) ? r : r - 5;
    }

    // Convert to string
    v = v + '';
    len = v.length - 2;

    // Return formatted string
    return v.substring(0, len) + '.' + v.substring(len);
}

Although the post is a bit old, the question isn't.
I think what the poster meant was, that some currency calculate with cents, but the prices are given in x.00 or x.05 cents. For example Swiss currency.

The solution, borrowed from RobG's answer with a little fix:

function roundToFiveCents(v) {
    var len;
    // Format to 2 numbers after decimal point an turn dollars into cents
    v = v.toFixed(2) * 100;

    // Get residual cents
    var r = v % 5;

    // Round to 5c
    if (r) {
        v -= (r === 1 || r === 2) ? r : r - 5;
    }

    // Convert to string
    v = v + '';
    len = v.length - 2;

    // Return formatted string
    return v.substring(0, len) + '.' + v.substring(len);
}
奈何桥上唱咆哮 2024-12-12 14:54:08

下面的 JavaScript 怎么样:

function my_round(x) {
    x *= 20;
    x = Math.ceil(x);
    return x / 20;
}
var amount = 3.37;
alert(my_round(amount));

How about the following javascript:

function my_round(x) {
    x *= 20;
    x = Math.ceil(x);
    return x / 20;
}
var amount = 3.37;
alert(my_round(amount));
私藏温柔 2024-12-12 14:54:08
var n = 8.22354;
roundx(n, 3)

function roundx(num, decPlace) {
  return Math.round(num * Math.pow(10, decPlace))/Math.pow(10, decPlace);
}

这个简单的函数非常适合舍入到 x 位。

var n = 8.22354;
roundx(n, 3)

function roundx(num, decPlace) {
  return Math.round(num * Math.pow(10, decPlace))/Math.pow(10, decPlace);
}

This simple function works well for rounding to x number of places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文