Rounding numbers in programming for WEB

In mathematics, situations often arise when the full accuracy of a number is not needed or may interfere due to the impossibility of determining. Such situations may arise when working, for example, with complex motion functions.

Rounding numbers

Rounding numbers to a certain sign is its replacement with a similar value with the addition of the last one with zeros at the end.

Natural fractions can be subjected to this operation to a different degree of accuracy: tens, hundreds, thousands, etc. Depending on the required degree of accuracy, it is necessary to replace with zeros the digit in the digit to which rounding is performed. Using simple examples, you can imagine this operation.

So, when rounding a number to tens, zero it is necessary to replace the digit in the category of unity. At the same time, when rounding to hundreds - it is necessary to replace the discharge of units and tens.

This operation involves obtaining an approximate value by conversion.

PHP rounding numbers

Although PHP is a translator language designed to simplify the work of creating WEB pages and applications, it also contains a number of functions for working with mathematical expressions. Their rounding is also embedded in this development environment.

PHP functions

PHP provides three functions for rounding numbers: round, ceil, and floor. The first is for rounding to the nearest integer. The second is the same as the first, only in a big way. The third - in a smaller direction.

The Round () function is defined by the following syntax:

float round (float value [, int precision])

The first parameter indicates the number over which the conversion takes place. The second, being optional, indicates the accuracy with which the numbers are rounded.

Function usage example:

Rounding php numbers

$ roun = round (3.8); // 4
$ roun = round (3.15); // 3
$ roun = round (5.5); // 6
$ roun = round (8.499); // 8
$ roun = round (2.46,1); // 2.5
$ roun = round (3.7384,3); // 3.738
$ roun = round (1939, -1); // 1940
$ roun = round (2.5,1); // 2.5
$ roun = round (1444, -2); // 1400

Rounding the desired number to hundredths:

$ roun = round (3.467,2); // 3.47

Rounding the desired number to an integer value:

$ roun = round (4.827); // 5

If you need to get the rounding of the number up, you should use the ceil () function given by the syntax:

float ceil (float value)

This function needs to be passed only one parameter containing a fractional number.

Function usage example:

$ cei = ceil (4.7); // 5
$ cei = ceil (4.001); // 4
$ cei = ceil (6.5); // 7
$ cei = ceil (3.0); // 3

If you need to get rounding numbers down, you need to

Rounding javascript numbers

use the floor () function, given by the syntax:

float floor (float value)

This function is similar to the previous one, except that it rounds the fractional number passed to it to a smaller integer value.

Function usage example:

$ okr = floor (4.99); // 4
$ okr = floor (5.023); // 5
$ okr = floor (6.4); // 6
$ okr = floor (7.0); // 7

JavaScript rounding

JavaScript, like PHP, has functions for rounding numbers. They are similar to PHP functions in both name and content, except that they are called as methods of a Math object.

JavaScript is essentially an object oriented programming language. From here some features of work on functions follow. The functions of rounding numbers that interest us and their properties are embedded in the Math object. And to call them, after the name of the object, you must specify the call operator ".", And then the name of the property. Similarly, you must specify the values ​​passed by this property.

Example:

alert ('Math.floor (125.6768) =' + Math.floor (125.6768));

Will show in a popup window 125.

In any case, even with the seemingly seeming complexity of working with object-oriented languages, there is no problem using JavaScript functions.

Source: https://habr.com/ru/post/K20602/


All Articles