To sum up the considerations for numerical calculation in PHP, the php Value

Source: Internet
Author: User

To sum up the considerations for numerical calculation in PHP, the php Value

I. Rounding

1. round-round the floating point number

float round ( float $val [, int $precision ] )

2: floor-rounding (down)

float floor ( float $value )

3. ceil-one-to-one method to get an integer (rounded up)

float ceil ( float $value )

Pitfalls:When the value is an integer, for example, 11, floor (11) = 10, ceil (11) = 12; the problem is obvious, so pay special attention to this during calculation.

Solution:Floor (11 + 0.01 );

2. multiplication and division of integers and Decimals

Pitfalls:3.5*100 = 300, even if you have good mathematics, it is useless. In the php world, this is the truth. If you use php's +-*/to calculate floating point numbers, some problems may occur due to incorrect computing results, such as echo intval (0.58*100), printing 57 instead of 58. This is actually a bug where the underlying binary of the computer cannot accurately represent floating point numbers, it is cross-language. I also encountered this problem using python. Therefore, most languages provide precision computing class libraries or function libraries. For example, php has a high-precision BC function library.

Solution:Is to change the integer to floating point 3.5 * (float) 100 = 350

Third, the floating point is weird.

Pitfalls:8.50-8 = 0.500000001 in fact, the reason is similar to 2

Solution:Round () the result to improve accuracy.

Summary

The above is all the considerations for PHP Data computing. I hope this article will help you with data computing in PHP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.