http://twpug.net/modules/newbb/viewtopic.php?post_id=10083
Just popping in 註冊日期: 文章: |
$a="1181745772.201471"; $c=$b-$a; 以上的程式 理論說 $b-$a 的答案 應該是 0.320969 但是 我跑出來 卻是 0.320968866348 請教各位 我是否要做什麼變數的型態轉換 或是使用什麼運算函數之類的方法 才能計算出正確的結果 發表日期:2007/6/20 15:06 |
|
工具箱 |
|
sam0228 |
回覆: 小數點的運算 |
#2 |
---|---|---|
網站管理員 註冊日期: 文章: |
可以用number_format限定顯示的小數 http://tw2.php.net/manual/en/function.number-format.php 發表日期:2007/6/20 15:33 |
|
工具箱 |
|
dowdot |
回覆: 小數點的運算 |
#3 |
---|---|---|
Just popping in 註冊日期: 文章: |
感謝您的建議 不過 我後來找了很久 找到了另一個解決的方式 運用 BC 高精確度函式庫 就是在編譯php時 加入以下的參數 --enable-bcmath 之後,就可以使用bcsub、bcadd、bcdiv等函數來運算 這樣,就可以將程式改寫成 $a="1181745772.201471"; 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 :
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ‘,‘, ‘ ‘);
// 1 234,56
$number = 1234.5678;
// english notation without thousands seperator
$english_format_number = number_format($number, 2, ‘.‘, ‘‘);
// 1234.57
?>
See Also
- money_format() - Formats a number as a currency string
- sprintf() - Return a formatted string
- printf() - Output a formatted string
- sscanf() - Parses input from a string according to a format
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);
?>
阅读(1725) | 评论(0) | 转发(0) |
0
上一篇:微软首宗针对中国大企业盗版案宣判:获赔217万
下一篇:Linux C编程一站式学习
相关热门文章
- A sample .exrc file for vi e...
- IBM System p5 服务器 HACMP ...
- 游标的特征
- busybox的httpd使用CGI脚本(Bu...
- Solaris PowerTOP 1.0 发布
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
热门推荐
-->
给主人留下些什么吧!~~
评论热议
小數點的運算[討論區- PHP新手區] : 台灣PHP聯盟