Posted By

sekihin on 03/09/09


Tagged

php number format


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

rezzz


number_format_nozero


 / Published in: PHP
 

  1. /*
  2. 意味:数値をカンマ表示させる、小数部分の最後でゼロが表示しない
  3. 引数:
  4.   name 数値
  5.   dec 精度
  6.   point 小数点
  7.  sep 区切り文字
  8. 返り値:
  9.   数字を、3桁ごとにカンマのついた形式表示に変換したもの。
  10. */
  11. function number_format_nozero($name, $dec = 0, $point = ".", $sep = "") {
  12. $name = number_format($name, $dec, $point, "");
  13. while(strpos($name,$point)!= false && substr($name,strlen($name)-1,1)=="0"){
  14. $name = substr($name, 0, strlen($name) - 1);
  15. $dec = $dec - 1;
  16. }
  17. echo number_format($name, $dec, $point, $sep);
  18. }
  19.  
  20. /*意味:数値をカンマ表示させる、小数部分の最後でゼロが表示しない引数: name 数値 dec 精度 point 小数点 sep 区切り文字返り値: 数字を、3桁ごとにカンマのついた形式表示に変換したもの。*/function number_format_nozero($name, $dec = 0, $point = ".", $sep = "") { $name = number_format($name, $dec, $point, ""); while(strpos($name,$point)!= false && substr($name,strlen($name)-1,1)=="0"){ $name = substr($name, 0, strlen($name) - 1); $dec = $dec - 1; } echo number_format($name, $dec, $point, $sep);}
  21.  
  22. echo number_format(123456.1)."<br>";
  23. echo number_format(123456.1,3)."<br>";
  24. echo number_format(123456.1,3,".",",")."<br>";
  25. echo "<hr>";
  26. echo number_format_nozero(123456.1)."<br>";
  27. echo number_format_nozero(123456.1,3)."<br>";
  28. echo number_format_nozero(123456.1,3,".",",")."<br>";
  29.  
  30. //●実行結果
  31. //123,456
  32. //123,456.100
  33. //123,456.100
  34. //--------------------------------------------------------------------------------
  35. //123456
  36. //123456.1
  37. //123,456.1

Report this snippet  

You need to login to post a comment.