search
Homephp教程php手册PHP四舍五入精确小数位及取整的方法

本篇文章将使用php对数字进行四舍五入保留N位小数,以及使用php对数字进行取整的方法做个小总结。

(1)php保留三位小数并且四舍五入

$num=0.0215489;
echo sprintf("%.3f", $num); // 0.022

(2)php保留三位小数不四舍五入

$num=0.0215489;
echo substr(sprintf("%.4f", $num),0,-1); // 0.021

(3)php进一法取整数(这个在分页程序的页数程序里面会用到)

echo ceil(4.3);    // 5
echo ceil(9.999);  // 10

(4)php舍去法取整数

echo floor(4.3);   // 4
echo floor(9.999); // 9

您可能感兴趣的文章

  • php统计在线人数,精确的统计在线人数的办法
  • 在php中分别使用curl的post提交数据的方法和get获取网页数据的方法总结
  • php最精确的字符串长度截取函数
  • PHP压缩html网页代码减小网络数据传输量,清除空格,制表符,注释标记
  • php中数组的并集、交集和差集函数介绍
  • 强大的PHP 图片处理类(水印、透明度、缩放、锐化、旋转、翻转、剪切、反色)
  • php压入元素到数组头部(array_unshift的用法)
  • php利用array_flip实现数组键值交换去除数组重复值



Statement
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

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software