In PHP, the processing of floating point numbers has always been a headache. This is because the representation of floating point numbers in computers is limited, and many calculations of floating point numbers will produce errors. Among them, in the output, we often want to keep only the few decimal places and remove the unnecessary parts. This article introduces several methods of removing decimals in PHP.
1: Use the number_format function
number_format() function can format numbers into a common thousands separator format. It also controls the number of digits displayed after the decimal point. We can achieve the effect of retaining decimals and removing decimals by passing two parameters in the number_format function. The first parameter is the number to be formatted, and the second parameter is the number of decimal places to retain.
<?php // 保留两位小数 $num = 3.1415926; echo number_format($num, 2); // 3.14 // 去掉小数 $num = 3.1415926; echo number_format($num, 0); // 3 ?>
Two: Use the round function
Use the round function to round floating point numbers to the specified precision and return the result. The first parameter is the number to be rounded, and the second parameter is the number of decimal places to be retained.
<?php // 保留两位小数 $num = 3.1415926; echo round($num, 2); // 3.14 // 去掉小数 $num = 3.1415926; echo round($num, 0); // 3 ?>
Three: Use the intval function
intval can convert a string or floating point number into an integer while removing the decimal part. It should be noted that intval automatically performs rounding down when converting floating point numbers.
<?php $num = 3.1415926; echo intval($num); // 3 ?>
Four: Use the floor function
The floor function can round a floating point number down to the nearest integer value. It is also important to note that floor will automatically perform rounding down when converting floating point numbers.
<?php $num = 3.7; echo floor($num); // 3 ?>
In actual development, we often need to decide how many decimal places to retain or whether to remove decimals based on business needs. Through the methods introduced above, you can easily retain and remove decimals, thereby bringing more convenience to your project.
The above is the detailed content of How to remove the content after the decimal point in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
