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

Just popping in

註冊日期:
2007/6/10 17:43
文章: 5

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

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

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

但是 我跑出來 卻是 0.320968866348

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

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

才能計算出正確的結果

發表日期:2007/6/20 15:06
工具箱
sam0228
回覆: 小數點的運算
#2
網站管理員

註冊日期:
2005/9/8 17:37
文章: 663
可以用number_format限定顯示的小數
http://tw2.php.net/manual/en/function.number-format.php
發表日期:2007/6/20 15:33
工具箱
dowdot
回覆: 小數點的運算
#3
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 :


$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

?>

Report a bug

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
ord nl2br


Last updated: Fri, 16 Apr 2010
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.

$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
28-Mar-2010 09:18

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

?>

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

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聯盟相关推荐

  1. (转)[超頻測試] 頂級龍二965的浮點運算速度因外頻上300而爆走16%

    http://www.coolaler.com/showthread.php?t=224405 [超頻測試] 頂級龍二965的浮點運算速度因外頻上300而爆走16% 目前頂級Phenom II X4 ...

  2. 免費玩雲端運算,Amazon Web Service 雲端運算平台攻略 【1】

    Amazon是全球雲端運算平台提供商的領頭羊之一.其雲端服務透過AWS(Amazon Web Services)網站 平台註冊.登入.使用.付費,單一入口相當便利. AWS平台提供了運算用的EC2 , ...

  3. php intval 运算,PHP Intval 的 8、7 運算 8、7 問題

    October 16, 2020 3 min to read PHP Intval 的 8.7 運算 8.7 問題 欸欸.乾太,你 php 小數計算會使用套件嗎?還是使用原生的方法進行運算?因為我遇到 ...

  4. Amazon Web Service 雲端運算平台攻略 【2】

    Sting Tao   5 November 2010 請確認 並且,已經開通了AWS帳號及收到Amazon的Signup confirmation的電子郵件. 本文目標 在免費的AWS帳號下,建立一 ...

  5. Big Data應用:以玩家意見之數據分析來探討何謂健康型線上遊戲(上)

    首先,所有資料都可以從網路上找到,只是我做了一些分析與整理而已.純粹分享心得~~ 最近再做研究的時候我跟我的同事K先生在某次偶然的討論中發現了一件有趣的事情. [疑~~~~~~~新楓之谷的玩家人氣指數 ...

  6. Amazon Web Service 雲端運算平台攻略 【3】:免費架WordPress部落格的虛擬主機

    本文目的 如果你需要一個能自己管理控制的部落格平台,基本上需要的是租用一台伺服器加上安裝部落格軟體.本文要告訴你,如何運用Amazon的雲端平台AWS提供的一年免費Free Tier,來安裝使用屬於你 ...

  7. OpenCV Tutorial: 開運算、閉運算(morphologyEx、MORPH_OPEN、MORPH_CLOSE)

    開運算.閉運算(morphologyEx.MORPH_OPEN.MORPH_CLOSE) 這邊介紹開運算和閉運算,這兩種都是侵蝕和膨脹複合而成,開運算是先侵蝕後膨脹,閉運算是先膨脹後侵蝕. 對於集合I ...

  8. 開運算、閉運算(morphologyEx、MORPH_OPEN、MORPH_CLOSE)

    這邊介紹開運算和閉運算,這兩種都是侵蝕和膨脹複合而成,開運算是先侵蝕後膨脹,閉運算是先膨脹後侵蝕. 對於集合I和H,假設使用H對I進行開運算,代表H對I進行侵蝕後膨脹,記作: 開運算可以使物體輪廓變得 ...

  9. 我推崇的流行唱法:美國Speech Level Singing的介紹和討論(推荐)

    Speech Level Singing 是近年來在美國很流行的一種聲樂教法.創始人Seth Riggs 原本是唱歌劇的,因為不滿意美國現在的聲樂教育而自創了一套練習方法,結果桃李滿天下(在美國,歐洲 ...

最新文章

  1. Samba+lamp完成指定任务
  2. Discuz! X3.2新增管理员无法登录后台的解决办法
  3. sping中@import
  4. 3线程的终止方式,线程属性,NPTL
  5. mysql 查询二维数组_sql数据库查询结果存入二维数组,结果都是0?求帮助
  6. 话剧《白鹿原》:叫人乍舌,令人唏嘘,也让人捏把汗(图)
  7. vim编程 插入 保存不退出 保存退出 退出不保存 另存为其他文件名 保存覆盖现有文件...
  8. tomcat调优方案Maximum number of threads (200) created for connector with address null and port 8091...
  9. 移动前端框架重构几个关键问题
  10. adam算法效果差原因_信电学院本科生提出AI新算法:速度比肩Adam,性能媲美SGD,ICLR领域主席赞不绝口...
  11. 程序员离无人值班有多远?
  12. hadoop fs ,hadoop dfs 和 hdfs dfs的区别以及HDFS的shell操作
  13. 【Spring-AOP】Spring提供的AOP开发方式和底层AOP开发方式
  14. Shell日常巡检脚本大全
  15. 大话西游2人气稳定服务器,大话西游2开服18年的老区还得排队,凌烟阁确实够火...
  16. DhtmlxGrid第一次接触,jsp页面
  17. 自定义身份证输入键盘
  18. OpenCV 保存视频
  19. (转载)constnbsp;charnbsp;*amp;nbs…
  20. 嵌入式自动判断是否插入网线的几种方法

热门文章

  1. linux下ipvsadm命令,命令手册:Linux中的ipvsadm命令
  2. redhat linux 硬件信息查看
  3. 未来十年,有哪些职业会消失...Python人工智能也会被替代吗
  4. 小米盒子增强版开箱:首款4K高清盒子
  5. 8400mg显卡,N卡的显卡门
  6. vb.net 教程 4-2 目录操作 DirectoryInfo 1
  7. Python爬虫百度首页
  8. Mac下如何在Scrivener 中插入MathType公式
  9. 未知USB设备(设备描述符请求失败)伴随笔记本蓝牙消失
  10. 记录 服装店进销存管理系统 需求分析资料