小數點的運算[討論區- PHP新手區] : 台灣PHP聯盟


http://twpug.net/modules/newbb/viewtopic.php?post_id=10083

Just popping in
註冊日期:
2007/6/10 17:43
文章: 5
<?php

$a="1181745772.201471";
$b="1181745772.522440";

$c=$b-$a;
echo $c;
?>

以上的程式 理論說 $b-$a 的答案 應該是 0.320969

但是 我跑出來 卻是 0.320968866348

請教各位 我是否要做什麼變數的型態轉換

或是使用什麼運算函數之類的方法

才能計算出正確的結果

發表日期:2007/6/20 15:06

應用擴展 工具箱


回覆: 小數點的運算
網站管理員
註冊日期:
2005/9/8 17:37
文章: 663
可以用number_format限定顯示的小數
http://tw2.php.net/manual/en/function.number-format.php

發表日期:2007/6/20 15:33

應用擴展 工具箱


回覆: 小數點的運算
Just popping in
註冊日期:
2007/6/10 17:43
文章: 5
感謝您的建議
不過 我後來找了很久
找到了另一個解決的方式

運用 BC 高精確度函式庫

就是在編譯php時 加入以下的參數

--enable-bcmath

之後,就可以使用bcsub、bcadd、bcdiv等函數來運算
也可以指定小數點之後的位數
感覺蠻方便的

這樣,就可以將程式改寫成

$a="1181745772.201471";
$b="1181745772.522440";

echo $c=bcsub($b,$a,6);

發表日期:2007/6/20 22:52






http://tw2.php.net/manual/en/function.number-format.php

Example #1 number_format() Example

For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. This is achieved with this line :

<?php

$number 
1234.56;

// english notation (default)
$english_format_number number_format($number);
// 1,235

// French notation
$nombre_format_francais number_format($number2','' ');
// 1 234,56

$number 1234.5678;

// english notation without thousands seperator
$english_format_number number_format($number2'.''');
// 1234.57

?>

notes-reject.gif See Also



ord> <nl2br
Last updated: Fri, 16 Apr 2010
 
notes-reject.gif add a note add a note User Contributed Notes
number_format
isedc at yahoo dot com
29-Mar-2010 08:38
Some programmers may have scripts that use the number_format function twice on a variable.  However, if a number is 4 or more digits, using the function twice with only the decimals parameter will lose part of the value that follows the thousands separator.

<?php
$var
= number_format(2234,2);
$var = number_format($var,2);
echo
$var;
# Expected Output: 2,234.00
# Actual Output: 2.00
?>

To fix, remove the thousands separator by setting the decimal point and thousands separator parameters like so:

<?php
$var
= number_format(2234,2,'.','');
$var = number_format($var,2,'.','');
echo
$var;
# Expected Output: 2234.00
# Actual Output: 2234.00
?>

If it's 3 digits or less, then it works normally with only the decimals parameter since there is no thousands separator.

<?php
$var
= number_format(123,2);
$var = number_format($var,2);
echo
$var;
# Expected Output: 123.00
# Actual Output: 123.00
?>
dipu dot ashok dot 17 at gmail dot com
28-Mar-2010 09:18
function to convert numbers to words
indian: thousand,lakh,crore
Note: function can only convert nos upto 99 crores

<?php
 $words
= array('0'=> '' ,'1'=> 'one' ,'2'=> 'two' ,'3' => 'three','4' => 'four','5' => 'five','6' => 'six','7' => 'seven','8' => 'eight','9' => 'nine','10' => 'ten','11' => 'eleven','12' => 'twelve','13' => 'thirteen','14' => 'fouteen','15' => 'fifteen','16' => 'sixteen','17' => 'seventeen','18' => 'eighteen','19' => 'nineteen','20' => 'twenty','30' => 'thirty','40' => 'fourty','50' => 'fifty','60' => 'sixty','70' => 'seventy','80' => 'eighty','90' => 'ninty','100' => 'hundred &','1000' => 'thousand','100000' => 'lakh','10000000' => 'crore');
function
no_to_words($no)
{    global
$words;
    if(
$no == 0)
        return
' ';
    else {          
$novalue='';$highno=$no;$remainno=0;$value=100;$value1=1000;       
            while(
$no>=100)    {
                if((
$value <= $no) &&($no  < $value1))    {
               
$novalue=$words["$value"];
               
$highno = (int)($no/$value);
               
$remainno = $no % $value;
                break;
                }
               
$value= $value1;
               
$value1 = $value * 100;
            }       
          if(
array_key_exists("$highno",$words))
              return
$words["$highno"]." ".$novalue." ".no_to_words($remainno);
          else {
            
$unit=$highno%10;
            
$ten =(int)($highno/10)*10;            
             return
$words["$ten"]." ".$words["$unit"]." ".$novalue." ".no_to_words($remainno);
           }
    }
}
echo
no_to_words(999978987);

?>

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1725) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
原文地址:https://www.cnblogs.com/ztguang/p/12647868.html