Home > Backend Development > PHP Tutorial > Solution to float to int distortion in PHP

Solution to float to int distortion in PHP

不言
Release: 2023-04-03 20:50:01
Original
2047 people have browsed it

The content of this article is about the solution to the distortion of converting float to int in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I encountered a problem at work:

The return value after payment is in cents, and the database uses yuan as the unit, so float is used;

For example, when it is returned, it is 3980, and the database data is 39.80. If you directly multiply this data by 100, it will become 3980;

I checked online and found that PHP has a weak type in float. For integers or after operations The result is a floating point number of an integer.

php can treat it as an integer, but the type remains unchanged. But if it is not an integer after operation, then PHP will treat the result as a strict floating point number.

Solution:

Use round before using intval

## Related recommendations:

How to use cURL to call WebService to obtain weather information (code)

TP5.1 How to use functions in view (code)

Code implementation of tp5 framework ajax asynchronously adding data

The above is the detailed content of Solution to float to int distortion in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template