/ / Rounding numbers in programming for WEB

Rounding numbers in WEB programming

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

Rounding Numbers

Rounding the numbers to a certain character is to replace it with a close value, with the last zeros at the end.

Natural shares can be subjected to thisoperations to a different degree of accuracy: tens, hundreds, thousands, etc. Depending on the required degree of accuracy, zeros must be replaced by the digit in the discharge to which the rounding is performed. On simple examples, you can imagine this operation.

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

This operation involves obtaining an approximate value by conversion.

Rounding PHP Numbers

Although the language of PHP is also a language-translator,designed to maximally simplify the creation of Web pages and applications, it also contains a number of functions for working with mathematical expressions. Their rounding is also sewn into this development environment.

PHP functions

In PHP, there are three functions for roundingnumbers: round, ceil and floor. The first is for rounding up to the nearest whole number. The second is the same as the first, only in the larger side. The third - in the smaller side.

The function Round () specifies the syntax:

float round (float value [, int precision])

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

An example of using the function:

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 required number to a decimal:

$ roun = round (3.467.2); // 3.47

Rounding the required number to an integer value:

$ roun = round (4.827); // 5

If you want to get the number rounded up, you should use the ceil () function with the syntax:

float ceil (float value)

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

An example of using the function:

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

If it is necessary to get the rounding of numbers to a smaller,

Rounding javascript numbers

use the floor () function, defined 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.

An example of using the function:

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

Rounds of JavaScript Numbers

In JavaScript, as in PHP, there are functions for rounding numbers. They are similar to PHP functions both in title and in content, except that they are called as methods of the Math object.

JavaScript is essentially an objectoriented programming language. Hence some features of work on functions follow. The functions of number rounding and their properties that interest us are laid in the Math object. And to call them after the name of the object, you must specify the "." 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 pop-up window 125.

In any case, even with the seemingly difficult work with object-oriented languages, there is no problem in using JavaScript functions.

</ p>>
Read more: