如何使用Math.round将数字舍入到最接近的偶数? [英] How to use Math.round to round numbers to the nearest EVEN number?

查看:196
本文介绍了如何使用Math.round将数字舍入到最接近的偶数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请仅使用JavaScript。

Using JavaScript only please.

这是我第一次尝试自己编写JavaScript。我成功地操纵了朋友们过去为我写过的代码,但是我从来没有从头开始编写自己的代码,也没有花时间尝试直到最近才理解语言本身。

This is the first time I am attempting to write JavaScript on my own. I've successfully manipulated codes that friends have written for me in the past, but I have never written my own from scratch, nor took the time to try and understand the language itself until recently.

我正在尝试制作一个基本的胸罩尺寸计算器,它从用户输入中获取数字(测量值),将它们引导到一个函数中并向用户返回(计算)胸罩尺寸。

I'm trying to make a basic bra size calculator that takes numbers (measurements) from user input, routes them into a function and returns (calculates) a bra size to the user.

由于我对这种语言很陌生,我现在只想写一个部分 - 乐队大小

Since I'm so new to this language, I'm only trying to write one part right now - "band size"

I有一个输入字段供用户输入我的胸围测量,我目前已设置为圆形。这按预期工作。见这里

I have an input field for users to type in their "under bust measurement" which I currently have set to round. This works as it intended. See here

<html>

<head>

<script type="text/javascript">

 function calculate() 
  {
   var underbust = document.getElementById("underBust").value;

    if (underbust.length === 0) 
     {
      alert("Please enter your underbust measurement in inches");
      return;
     }

    document.getElementById("bandsize").innerHTML = Math.round(underbust);
   }

</script>

</head>

<body>
<input type="number" id="underBust" /> inches<br>
<input type="submit" value="Submit" onclick="calculate()" /><br>
<b>underbust:</b> <div id="bandsize">bandsize will appear here</div><br>
</body>

</html>

但是,我不需要输入'underBust'来舍入到最接近的整数。我需要它来舍入到最接近的偶数整数,因为胸罩带尺寸只有整数。

However, I don't need the input 'underBust' to round to the nearest whole number. I need it to round to the nearest EVEN whole number, since bra band sizes only come in even whole numbers.

例如,如果用户输入数字31.25代码目前将其舍入为31,但我需要它将其四舍五入为32

For example, if the user inputs the number "31.25" the code would currently round it to "31" but I need it to round it to "32"

如果用户输入数字30.25,代码将围绕它正确到30因为在这种情况下最接近的整数和最接近的整数偶数相同。但是,如果用户输入30.5,代码会将其四舍五入为31,但我仍然需要将其向下舍入为30

If the user inputs the number "30.25" the code would round it correctly to "30" because the nearest whole number and nearest whole even number are the same in this case. However, if the user inputs "30.5" the code would round it up to "31" but I still need it to round down to "30"

基本上,我需要如果用户输入等于或大于奇数(29.00变为30,31.25变为32等),则要向上舍入的数字。如果用户输入大于或等于偶数且小于下一个奇数(28,28.25,28.75等),我需要将其向下舍入(在前面的例子中,对于所有情况为28)。奇数是舍入的中间分隔,而不是任何数字的.5。

Basically, I need the number to be rounded UP if the user input is equal to or greater than an odd number (29.00 becomes 30, 31.25 becomes 32, etc.). If the user input is greater than or equal to an even number and less than the next odd number (28, 28.25, 28.75, etc.) I need it to round down (in the previous example, to 28 for all cases). The odd number is the middle divide for rounding, instead of the ".5" of any number.

这可能吗?

推荐答案

这应该这样做:

2 * Math.round(underbust / 2);

这篇关于如何使用Math.round将数字舍入到最接近的偶数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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