ComputersProgramming

Rounding numbers in WEB programming

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

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

Natural shares can be subjected to this operation 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 rounding up the number to tens, zero is necessary to replace the digit in the digit of the unit. 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 PHP language is a compiler language designed to 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

PHP provides three functions for rounding numbers: round, ceil, and floor. The first is for rounding 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:

$ 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 hundredth:

$ 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 needs to pass 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,

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 inherently an object-oriented programming language. Hence some features of work on functions follow. The functions of rounding of 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 the 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.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.