将浮点数舍入为-1,0或1

时间:2017-04-26 11:13:33

标签: c# rounding

我需要将浮点数舍入为-1或1.如果浮点数等于0,则将其保持为零。 现在我用这个:

<?php require_once('Connections/connbowling.php'); ?>

<?php
if (!isset($_SESSION)){
    session_start();
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_bowlingl, $connbowling);
$query_rsWedstrijd = "SELECT * FROM FILES";
$rsWedstrijd = mysql_query($query_rsWedstrijd, $connbowling) or die(mysql_error());
$row_rsWedstrijd = mysql_fetch_assoc($rsWedstrijd);
$totalRows_rsWedstrijd = mysql_num_rows($rsWedstrijd);
$query_rsWedstrijd = "SELECT * FROM FILES";
$rsWedstrijd = mysql_query($query_rsWedstrijd, $connbowling) or die(mysql_error());
$row_rsWedstrijd = mysql_fetch_assoc($rsWedstrijd);
$totalRows_rsWedstrijd = mysql_num_rows($rsWedstrijd);

$editFormAction = $_SERVER['PHP_SELF'];
if(!empty($_FILES))
{
    $filename    =current(explode('.', $_FILES['input']['name']));
    $filetype    = $_FILES['input']['type'];
    $filesize     = $_FILES['input']['size'];
    $tmpname    = $_FILES['input']['tmp_name'];
    $content    = file_get_contents($tmpname);
    $fp                = fopen($tmpname,'r');
    $content     = fread($fp,$filesize);
    $content     = addslashes($content);
    $date       =$_POST['date1'];
    include("Connections/connbowling.php");
    $query="INSERT INTO FILES (FILENAME,FILETYPE,FILESIZE,CONTENT,DATUM) VALUES ('".$filename."','".$filetype."','".$filesize."','".$content."','".$date."');";

if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO files (`YEAR`, `DAY`) VALUES (%s, %s)",
                       GetSQLValueString($_POST['year'], "text"),
                       GetSQLValueString($_POST['DAY'], "text"));

  mysql_select_db($database_bowlingl, $bowlingl);
  $Result1 = mysql_query($insertSQL, $connbowling) or die(mysql_error());
$insertGoTo = "http://www.xxxxxxxxxxxxxxxxx";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

}
    mysql_query($query) or die(mysql_error());
    echo("<table>\n");
    echo("<tr>\n");
    echo("\t<td>Bestandsnaam</td><td>".$filename."</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("\t<td>Type</td><td>".$filetype."</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("\t<td>Grootte</td><td>".$filesize."</td>\n");
    echo("</tr>\n");
    mysql_close();    
}   


?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtmlcodeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />



</head>



  <h3> Scores uploaden voor alleen de huisleague.!!</h3>
    <p><form action="<?php echo $row_rsWedstrijd['']; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
      <input type="file" name="input" id="input" />

     <link type="text/css" href="../jquery.ui-1.5.2/_css/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
    <script type="text/javascript" src="Scripts/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="Scripts/ui.datepicker.js"></script>

        <script type="text/javascript">

       $(document).ready(function(){
                             $("#datepicker").datepicker({changeMonth:true,changeYear:true,showButtonPanel:true});

    //setter
    $("#datepicker").datepicker('option','dateFormat','yy-mm-dd');
  });

  </script>
      <input name="date1" id="datepicker"  />
      <p>&nbsp;</p>
<input type="submit" name="submit" id="submit" value="versturen" />
    </form></p>
    <p>&nbsp;</p>



<script type="text/javascript">

</script>

有更优雅的解决方案吗?

1 个答案:

答案 0 :(得分:4)

您可以使用Math.Sign(x)执行此操作。当x = 0时它将返回0,如果x为负则返回-1,如果x为正则返回+1。我相信这会做你想要的。这里的文档:https://msdn.microsoft.com/en-us/library/z394hhsx(v=vs.110).aspx