当前位置:网站首页>Introduction to several methods of keeping two decimal places in PHP
Introduction to several methods of keeping two decimal places in PHP
2022-07-28 12:15:00 【History teacher-】
1. No carry
such as 3.149569 Take two decimal places , The last two can't be rounded . result :3.14.
have access to floor function
This function is rounding . for example ,floor(4.66456) result :4 .
floor(9.1254) result 9 .
therefore , Go to two decimal places , You need to multiply 100, Then round off and round off , Divided by 100, namely :
$a=floor(3.149569*100)/100
At this point, a floating-point calculation will appear bug, adopt php To solve this problem , First strval Convert to string , Then automatically identify the type . The following percentage calculation has been used strval() Function
Calculate percentage
$successRate = floor(strval((2/3)*10000))/10000*100; $result = $successRate.'%';
2. Rounding off
round function
float round ( float val [, int precision])
Return to val Specify the precision according to precision( The number of digits after the decimal point ) The result of rounding .
precision It can also be negative or zero ( The default value is ).
sprintf function
Achieve rounding , And if it doesn't have a decimal place , Will automatically use the specified characters ( designated 0) Add to specified decimal places
echo sprintf("%01.2f", 5.228); // 5.23
echo sprintf("%01.2f", 5.224); // 5.22
echo sprintf("%01.2f", 5); // 5.00number_format function
If it is without decimal places , Will use automatically 0 Add to specified decimal places
echo number_format(5.228,2); // 5.23 echo number_format(5.224,2); // 5.22 echo number_format(5,2); // 5.00
round function
This function can realize rounding , But if it doesn't have a decimal place , No decimal places after processing
echo round(5.228,2); // 5.23 echo round(5.224,2); // 5.22 echo round(5,2); // 5
php Take the whole thing in one way
echo ceil(4.4); // 5 echo ceil(4.6); // 5
php To round out by giving up
echo floor(4.4); // 4 echo floor(4.6); // 4
边栏推荐
- Traversal and copy of files in jar package
- Launcher sample code
- Top level "redis notes", cache avalanche + breakdown + penetration + cluster + distributed lock, Nb
- [diary of supplementary questions] [2022 Niuke summer school 2] h-take the elevator
- Laravel之缓存
- The principle and use of the wrap file of tolua
- Lua makes a deep copy of table
- 玩转诗词-领略古诗文之美
- Laravel $object->updated_at 返回的是Carbon对象,如何返回正常时间格式
- China business CDP white paper | love Analysis Report
猜你喜欢

Detailed deployment and configuration of CEPH cluster (II)

使用百度飞桨 EasyDL 完成垃圾分类

"Weilai Cup" 2022 Niuke summer multi school training camp 2

Lyscript get previous and next instructions

Full resolution of the use of go native plug-ins
![Opencv notes sorting [Hough transform]](/img/80/8f5b0d7e1c5adc39cb5404dcdb1b11.png)
Opencv notes sorting [Hough transform]

An idea plug-in helps you transform dto, VO, Bo, Po, do gracefully
![[leetcode] 7. valid anagram · effective letter ectopic words](/img/bc/9806df1358c6f09db03ef2e771aa5a.png)
[leetcode] 7. valid anagram · effective letter ectopic words

Tencent two sides: @bean and @component are used in the same class, what will happen?

15. User web layer services (III)
随机推荐
QT writing IOT management platform 42 data query export print
[diary of supplementary questions] [2022 Niuke summer multi school 2] l-link with level editor I
Test platform (V) knowledge points supplement
Article summary of MinGW installation and use
php保留两位小数的几种方法介绍
Force buckle 7_ 1672. Total assets of the richest customers
php 日期计算操作处理,当前日期加一天和指定日期减一天
“蔚来杯“2022牛客暑期多校训练营2
Loongarch Godson architecture document collection
Upgrading of computing power under the coordination of software and hardware, redefining productivity
分布式定时器
Simple selection sort and heap sort
Service workers let the website dynamically load webp pictures
Globalthis is not defined solution
Gecko competition 2.0 is new! Come and show your flexible operation skills!
Some knowledge concepts
顶级“Redis笔记”,缓存雪崩+击穿+穿透+集群+分布式锁,NB了
使用百度飞桨 EasyDL 完成垃圾分类
Untiy controls the playback speed of animation
游戏流程与底层实现 逐步完成