Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6543542
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类:

2010-04-23 18:47:18




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

$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
文章:
可以用number_format限定顯示的小數

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

應用擴展 工具箱


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

運用 BC 高精確度函式庫

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

--enable-bcmath

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

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

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

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

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








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 :


$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

?>

See Also

  • - Formats a number as a currency string
  • - Return a formatted string
  • - Output a formatted string
  • - Parses input from a string according to a format


ord> <nl2br
Last updated: Fri, 16 Apr 2010
 
add a note User Contributed Notes
number_format
isedc at yahoo dot com
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.

$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:

$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.

$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
function to convert numbers to words
indian: thousand,lakh,crore
Note: function can only convert nos upto 99 crores

 $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);

?>

阅读(3261) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~