PHP functions: rounding down

Mathematical functions are an essential component of any programming language. But this does not mean that when performing operations on numbers, it is necessary to use them exactly as the language developer intended. In PHP, rounding down can be done through string functions.

Necessity and rounding options

The reasons for rounding numbers can be very different. The most popular is the formation of numerical attributes for CSS styles and rules.

When creating modern sites, the requirement of dynamism is a must. The requirement of adaptability is desirable. This means that the recalculation of the size of images, tags and other components of the site’s frame must be done on the server when the page is formed.

Rounding for CSS attribute attributes




The basic options for rounding in any language are limited to three functions: mathematical, to a smaller or larger side.

In PHP, programming also has three functions for rounding operations: round (), floor (), and ceil ().

Programming (in a general sense) suggests multiplying the original number by the power of 10 (10, 100, 1000, 10000, etc.) to perform the rounding function in the desired position of the number. Regarding the round () function, this property does not matter (it has an optional second parameter, which can determine the rounding position).





PHP rounding example

In this example, a classic rounding of the same number by all three basic functions is given and a rounding option is proposed that allows lowercase semantics of the language. A number is also a string of characters. It's just that its alphabet is limited to ten digits, a dot, and the signs "+" and "-". If we consider the exponential representation, then there is still the letter E.

Rounding Examples




For the first number (1), the value of round () is the same as ceil (); for the second number (2), the results of round () and floor () are the same. Ideal PHP rounding down offers option (3).

Rounding results




Row rounding of a number is the idea of ​​dividing a number into two components by a point. The result is an array. The first element is the integer part of the number, the second element is fractional. If there is no point, then there is no second element. You can not even check.

For numbers in an exponential form, this idea is not good, but the main concern of programming is to process information. PHP rounding a number down is more in demand than mathematical or up. Apply explode () is beautiful and practical.




All Articles