PHP实现数字格式化,每三位加逗号
PHP实现数字格式化,每三位加逗号
<?php function num_format($num){ if(!is_numeric($num)){ return false; } $rvalue=''; $num = explode('.',$num);//把整数和小数分开 $rl = !isset($num['1']) ? '' : $num['1'];//小数部分的值 $j = strlen($num[0]) % 3;//整数有多少位 $sl = substr($num[0], 0, $j);//前面不满三位的数取出来 $sr = substr($num[0], $j);//后面的满三位的数取出来 $i = 0; while($i <= strlen($sr)){ $rvalue = $rvalue.','.substr($sr, $i, 3);//三位三位取出再合并,按逗号隔开 $i = $i + 3; } $rvalue = $sl.$rvalue; $rvalue = substr($rvalue,0,strlen($rvalue)-1);//去掉最后一个逗号 $rvalue = explode(',',$rvalue);//分解成数组 if($rvalue[0]==0){ array_shift($rvalue);//如果第一个元素为0,删除第一个元素 } $rv = $rvalue[0];//前面不满三位的数 for($i = 1; $i < count($rvalue); $i++){ $rv = $rv.','.$rvalue[$i]; } if(!empty($rl)){ $rvalue = $rv.'.'.$rl;//小数不为空,整数和小数合并 }else{ $rvalue = $rv;//小数为空,只有整数 } return $rvalue; }
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1378
52

