将变量舍入为最接近的整数

时间:2017-02-18 19:17:35

标签: javascript variables rounding

我有一些代码将变量舍入到最接近的整数 - 除了我似乎无法将变量舍入到最接近的整数。这是我的代码:

var score = 600;
var coinBase = 400;
var coinInt = score / coinBase;
var coin = round(VARIABLEcoinInt');

换句话说,我试图获取变量coinInt,将其四舍五入到最接近的整数,并将其输出到变量硬币中。

2 个答案:

答案 0 :(得分:1)

没有round功能。请改用Math.round功能。

var score = 600;
var coinBase = 400;
var coinInt = score / coinBase;
var coin = Math.round(coinInt);

console.log(coin);

答案 1 :(得分:0)

您可以使用3个基于 include "../connections/connect_mysqli.php"; $conn = dbConnect('read'); $sql = "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"; // query the person $result = $conn->query($sql) or die($conn->error); // ------- MAKE SURE PERSON EXISTS IN DATABASE --------- $existCount = $result->num_rows; // count the row nums if ($existCount == 0) { // evaluate the count echo "Your login session data is not on record in the database."; exit(); } ?> <?php // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php // Delete Item Question to Admin, and Delete Product if they choose if (isset($_GET['deleteid'])) { echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="inventory_list.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="inventory_list.php">No</a>'; exit(); } if (isset($_GET['yesdelete'])) { // remove item from system and delete its picture // delete from database $id_to_delete = $_GET['yesdelete']; $sql = "DELETE FROM products WHERE id='$id_to_delete' LIMIT 1"; $result = $conn->query($sql) or die($conn->error); // unlink the image from server // Remove The Pic ------------------------------------------- $pictodelete = ("../images/$id_to_delete.jpg"); if (file_exists($pictodelete)) { unlink($pictodelete); } header("location: inventory_list.php"); exit(); } ?> <?php // Parse the form data and add inventory item to the system if (isset($_POST['product_name'])) { $product_name = $conn->real_escape_string($_POST['product_name']); $price = $conn->real_escape_string($_POST['price']); $details = $conn->real_escape_string($_POST['details']); $details2 = $conn->real_escape_string($_POST['details2']); $details3 = $conn->real_escape_string($_POST['details3']); // See if that product name is an identical match to another product in the system $sql = "SELECT id FROM products WHERE product_name='$product_name' LIMIT 1"; $result = $conn->query($sql) or die($conn->error); $productMatch = $result->num_rows; // count the output amount if ($productMatch > 0) { echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>'; exit(); } // Add this product into the database now $sql = ("INSERT INTO products (product_name, price, details, details2, details3, date_added) VALUES('$product_name','$price','$details','$details2','$details3',now())") or die ($conn->error); $result = $conn->query($sql) or die($conn->error); $pid = $conn->insert_id; // Place image in the folder $newname = "$pid.jpg"; move_uploaded_file( $_FILES['fileField']['tmp_name'], "../images/$newname"); header("location: inventory_list.php"); exit(); } ?> 的功能之一。

Math - 舍入到最接近的整数:

Math.round

Math.round(1.4) // 1 Math.round(1.5) // 2 - 下降

Math.floor

Math.floor(1.2) // 1 Math.floor(1.9) // 1 - 累计

Math.ceil