php float浮点型数据应用详解

 更新时间:2016年11月25日 15:08  点击:2085

php教程 float浮点型数据应用详解

1 float round ( float $val [, int $precision ] ) 返回将 val 根据指定精度 precision (十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。

     echo round(4.3)  //4

2 string sprintf ( string $format [, mixed $args [, mixed $... ]] ) 返回格式化数据的字符串

view sourceprint?$a=12.338938438; 

echo sprintf("%.5f",$a) //结果:12.33894  

  

  

$a=12.3312356; 

echo sprintf("%.5f",$a);//12.33124 

echo sprintf("%f",$a);//331236  默认小数点后6位

 

3 string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )

view sourceprint?$number = 1234.5678; 

  

$english_format_number = number_format($number, 2, '.', ''); 

echo  $english_format_number ; // 1234.57

以上这些都自动做了四舍五入,有时候需求不需要四舍五入呢,怎么办,没有想到好办法,谁知道可以告诉一声。

自己写了个麻烦点的函数,记录下

view sourceprint?function getFloatValue($f,$len) 

  $tmpInt=intval($f); 

  

  $tmpDecimal=$f-$tmpInt; 

  $str="$tmpDecimal"; 

  $subStr=strstr($str,'.'); 

  if(strlen($subStr)<$len+1) 

 { 

  $repeatCount=$len+1-strlen($subStr); 

  $str=$str."".str_repeat("0",$repeatCount); 

  

 } 

  

  return    $tmpInt."".substr($str,1,1+$len); 

  

echo getFloatValue(12.99,4) //12.9900 

echo getFloatValue(12.9232555553239,4) //12.9232

array preg_split ( string pattern, string subject [, int limit [, int flags]])

返回一个数组,包含 subject 中沿着与 pattern 正则表达式来匹配的边界所分割的子串。

如果指定了 limit,则最多返回 limit 个子串,如果 limit 是 -1,则意味着没有限制,可以用来继续指定可选参数 flags。

flags 可以是下列标记的任意组合(用按位或运算符 | 组合):

PREG_SPLIT_NO_EMPTY
如果设定了本标记,则 preg_split() 只返回非空的成分

看个实例

<?php教程
$word_count = $word_length = 0;

if ($fh = fopen('novel.txt','r')) {
  while (! feof($fh)) {
    if ($s = fgets($fh)) {
      $words = preg_split('/s+/',$s,-1,PREG_SPLIT_NO_EMPTY);
      foreach ($words as $word) {
        $word_count++;
        $word_length += strlen($word);
      }
    }
  }
}

print sprintf("The average word length over %d words is %.02f characters.",
              $word_count,
              $word_length/$word_count);
?>

简单应用

<?
$user_info = "+J+++G+++++w";
$fields = preg_split("/+{1,}/", $user_info);
while ($x < sizeof($fields)) :
   print $fields[$x]. "<br>";
   $x++;
endwhile;
?>

由于分割后变成了数组,所以我们要利用foreach 来遍历输出了。

<?php
   $delimitedText = "+A+++G+++++++++++C";
   $fields = preg_split("/+{1,}/", $delimitedText);
    foreach($fields as $field) echo $field."<br />";
?>

<?
$text = "a, o, p";
$fruitarray = preg_split( "/, | and /", $text );
print "<pre>n";
print_r( $fruitarray );
print "</pre>n";
?>
 

$_FILES参考说明

Element                         Contains                                Example
 
$ FILES['fupload']['name']      文件名                   test.gif
 
$_FILES['fupload']['tmp_name']  临时文件名                  /tmp/asdfadsf
 
$_FILES['fupload']['size']      文件大小        6835
 
$_FILES['fupload']['error']     错误代码          UPLOAD_ERR_FORM_SIZE
 
$_FILES['fupload']['type']      文件类型    image/gif
 
 

shuffle() 函数把数组中的元素按随机顺序重新排列。

若成功,则返回 TRUE,否则返回 FALSE。

注释:本函数为数组中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。

注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已被自动完成

<?php教程
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
print_r($my_array);
?>

下面看一个// 用数组存放广告列表

$ads = array('<a href="#"><img src="ad-125x125.png" alt="广告 1" width="125" height="125" /></a>'
,'<a href="#"><img src="ad-125x125.png" alt="广告 2" width="125" height="125" /></a>'
,'<a href="#"><img src="ad-125x125.png" alt="广告 3" width="125" height="125" /></a>'
,'<a href="#"><img src="ad-125x125.png" alt="广告 4" width="125" height="125" /></a>'
);

// 对数组进行随机排序
shuffle($ads);

// 输出经过排序的数组
$html = '';
foreach ($ads as $ad) {
$html .= $ad;
}
echo $html;

关于用户登录这个我们不说多了,很简单的利用表单发送的数据给php教程处理,判断用户名密码是否与我们设置的或数据库教程的一样就可以了。 <?php session_start(); if (isset($_POST["submit"])) { if ($_POST["user"] == "php5" && $_POST["pass"] == "iscool") { $_SESSION["username"] = $_POST["user"]; } } ?> User Authentication <?php if (isset($_SESSION["username"])) { echo("You are logged in!"); } else { ?>


<?php } ?> 更安全的登录就是利用cookie操作如下 <?php unset($username); if ($_COOKIE['login']) { list($c_username,$cookie_hash) = split(',',$_COOKIE['login']); if (md5($c_username.$secret_word) == $cookie_hash) { $username = $c_username; } else { print "You have sent a bad cookie."; } } if ($username) { print "Welcome, $username."; } else { print "Welcome, anonymous user."; } ?>
[!--infotagslink--]

相关文章