PHP格式化MYSQL返回float类型的方法,phpmysql返回float
本文实例讲述了PHP格式化MYSQL返回float类型的方法。分享给大家供大家参考,具体如下:
PHP 中获取mysql的float字段,echo 输出后,小数部分为包含多个0.
可使用 floatval($num) 将0舍去。
如要保留小数位,可使用 number_format($num, 2);
number_format函数对超过指定位数的值,进行了四舍五入。
如不想四舍五入,而保留所有小数。可使用如下方法:
// 如仅想保留两位小数可用 number_format($num, 2); echo f('1001.334534', 2) . '<br>'; // 1001.334534 echo f('-1001.000', 2) . '<br>'; // -1001.00 echo f('1001.3', 5) . '<br>'; // 1001.30000 echo f('1001.33') . '<br>'; // 1001.33 echo f('1001.000') . '<br>'; // 1001 // 格式化小数,但不四舍五入,如有小数则全保留,无小数则添加0; function f($num, $v = 0) { $num = floatval($num); if ($v > 0) { $num = '' . $num; $arr = explode('.', $num); if (count($arr) === 1) { $num .= '.' . str_repeat('0', $v); } else { $v -= strlen($arr[1]); if ($v > 0) $num .= str_repeat('0', $v); } } return $num; }
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
- PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)
- PHP获取当前日期和时间及格式化方法参数
- php格式化电话号码的方法
- php实现格式化多行文本为Js可用格式
- PHP日期函数date格式化UNIX时间的方法
- php格式化金额函数分享
- PHP中常用的字符串格式化函数总结
- PHP文件大小格式化函数合集
- php格式化日期和时间格式化示例分享

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
